Ë
    S^(hç‚  ã                   óR  — d Z ddlZddlmZmZmZmZ ddlZddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZm Z m!Z!m"Z"m#Z#  e«       rddl$Z$ e"jJ                  e&«      Z'd	eeee      ee   ef   d
eee      fd„Z( G d„ de«      Z) G d„ de
«      Z*dgZ+y)zImage processor class for Fuyu.é    N)ÚDictÚListÚOptionalÚUnioné   )ÚBaseImageProcessorÚBatchFeatureÚget_size_dict)ÚpadÚresizeÚto_channel_dimension_format)
ÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚget_image_sizeÚinfer_channel_dimension_formatÚis_scaled_imageÚis_valid_imageÚmake_list_of_imagesÚto_numpy_arrayÚvalidate_preprocess_arguments)Ú
TensorTypeÚfilter_out_non_signature_kwargsÚis_torch_availableÚis_torch_deviceÚis_torch_dtypeÚloggingÚrequires_backendsÚimagesÚreturnc                 óÚ   — t        | «      r| ggS t        | t        «      rt        d„ | D «       «      r| S t        | t        «      r| D �cg c]  }t	        |«      ‘Œ c}S t        d«      ‚c c}w )Nc              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­w©N)Ú
isinstanceÚlist)Ú.0Úimages     úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/fuyu/image_processing_fuyu.pyú	<genexpr>z.make_list_of_list_of_images.<locals>.<genexpr>@   s   è ø€ Ò'TÀE¬
°5¼$×(?Ñ'Tùs   ‚zHimages must be a list of list of images or a list of images or an image.)r   r$   r%   Úallr   Ú
ValueError)r   r'   s     r(   Úmake_list_of_list_of_imagesr,   :   se   € ô �fÔØ�ˆzÐä�&œ$Ô¤CÑ'TÈVÔ'TÔ$TØˆä�&œ$ÔØ8>Ö?¨uÔ# EÕ*Ò?Ð?ä
Ð_Ó
`Ð`ùò @s   ÁA(c                   ó6   — e Zd ZdZddeeeef      fd„Zdd„Z	y)ÚFuyuBatchFeaturez¤
    BatchFeature class for Fuyu image processor and processor.

    The outputs dictionary from the processors contains a mix of tensors and lists of tensors.
    NÚtensor_typec                 ó¬  ‡‡‡‡	— |€| S | j                  |¬«      \  ŠŠˆˆfd„Šˆˆ	fd„}| j                  «       D ]‡  \  Š	}t        |t        «      r=t        |d   t        «      r*|D ��cg c]  }|D �cg c]
  } ||«      ‘Œ c}‘Œ c}}| ‰	<   ŒSt        |t        «      r|D �cg c]
  } ||«      ‘Œ c}| ‰	<   Œ} ||«      | ‰	<   Œ‰ | S c c}w c c}}w c c}w )a5  
        Convert the inner content to tensors.

        Args:
            tensor_type (`str` or [`~utils.TensorType`], *optional*):
                The type of tensors to use. If `str`, should be one of the values of the enum [`~utils.TensorType`]. If
                `None`, no modification is done.
        )r/   c                 ó(   •—  ‰| «      r| S  ‰| «      S r#   © )ÚelemÚ	as_tensorÚ	is_tensors    €€r(   Ú_convert_tensorz<FuyuBatchFeature.convert_to_tensors.<locals>._convert_tensor^   s   ø€ Ù˜ŒØ�Ù˜T“?Ð"ó    c                 óV   •— 	  ‰| «      S #  ‰dk(  rt        d«      ‚t        d«      ‚xY w)NÚoverflowing_valueszKUnable to create tensor returning overflowing values of different lengths. zUnable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.)r+   )r3   r6   Úkeys    €€r(   Ú_safe_convert_tensorzAFuyuBatchFeature.convert_to_tensors.<locals>._safe_convert_tensorc   sB   ø€ ðÙ& tÓ,Ð,øðØÐ.Ò.Ü$Ð%rÓsÐsÜ ðXóð ús   ƒ ‹(r   )Ú_get_is_as_tensor_fnsÚitemsr$   r%   )
Úselfr/   r;   ÚvalueÚelemsr3   r6   r4   r5   r:   s
         @@@@r(   Úconvert_to_tensorsz#FuyuBatchFeature.convert_to_tensorsP   sÔ   û€ ð ÐØˆKà#×9Ñ9ÀkÐ9ÓRÑˆ	�9õ	#õ
		ð Ÿ*™*›,ò 		8‰JˆC�Ü˜%¤Ô&¬:°e¸A±hÄÔ+EàY^×_ÐPUÀUÖK¸TÑ2°4Õ8ÔKÓ_��S’	Ü˜E¤4Ô(àDIÖJ¸DÑ1°$Õ7ÒJ��S’	ñ 1°Ó7��S’	ð		8ð ˆùò LùÓ_ùò Ks   Á(	CÁ1CÂ CÂ!CÃCc           
      óž  ‡‡‡‡— t        | dg«       ddlŠi }‰j                  d«      Š‰€et        ‰«      dkD  rW‰d   }t	        |«      rnFt        |t        «      st        |«      st        |t        «      r|Šnt        dt        |«      › d�«      ‚ˆˆˆˆfd„}| j                  «       D ]‘  \  }}t        |t        «      rGt        |d   t        «      r4g }|D ]'  }	|j                  |	D �
cg c]
  }
 ||
«      ‘Œ c}
«       Œ) |||<   Œ]t        |t        «      r|D �
cg c]
  }
 ||
«      ‘Œ c}
||<   Œ‡ ||«      ||<   Œ“ || _        | S c c}
w c c}
w )a  
        Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in
        different `dtypes` and sending the `BatchFeature` to a different `device`.

        Args:
            args (`Tuple`):
                Will be passed to the `to(...)` function of the tensors.
            kwargs (`Dict`, *optional*):
                Will be passed to the `to(...)` function of the tensors.

        Returns:
            [`BatchFeature`]: The same instance after modification.
        Útorchr   NÚdevicez*Attempting to cast a BatchFeature to type z. This is not supported.c                 óx   •—  ‰j                   | «      r | j                  ‰i ‰¤ŽS ‰�| j                  ‰¬«      S | S )N)rD   )Úis_floating_pointÚto)r3   ÚargsrD   ÚkwargsrC   s    €€€€r(   Ú_toz FuyuBatchFeature.to.<locals>._to›   sE   ø€ à&ˆu×&Ñ& tÔ,à�t—w‘w Ð/¨Ñ/Ð/ØÐ!Ø—w‘w f�wÓ-Ð-àˆKr7   )r   rC   ÚgetÚlenr   r$   Ústrr   Úintr+   r=   r%   ÚappendÚdata)r>   rH   rI   Únew_dataÚargrJ   ÚkÚvÚnew_vr@   r3   rD   rC   s    ``        @@r(   rG   zFuyuBatchFeature.to{   s?  û€ ô 	˜$  	Ô*ÛàˆØ—‘˜HÓ%ˆàˆ>œc $›i¨!šmà�q‘'ˆCÜ˜cÔ"àÜ˜C¤Ô%¬¸Ô)=ÄÈCÔQTÔAUØ‘ô !Ð#MÌcÐRUËhÈZÐWoÐ!pÓqÐq÷	ð —J‘J“Lò 	%‰DˆAˆqÜ˜!œTÔ"¤z°!°A±$¼Ô'=à�Øò @�EØ—L‘L¸Ö!>°¡# d¥)Ò!>Õ?ð@à#�˜’Ü˜AœtÔ$à56Ö7¨T™s 4�yÒ7�˜’á! !›f�˜’ð	%ð ˆŒ	Øˆùò "?ùò 8s   Ã'EÄE
r#   )r    r	   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rM   r   rA   rG   r2   r7   r(   r.   r.   I   s'   „ ññ)¨h°u¸SÀ*¸_Ñ7MÑ.Nó )ôV8r7   r.   c            !       óp  ‡ — e Zd ZdZg d¢Zddej                  dddddddddfd	ed
ee	e
ef      dededede
dedeeee   f   deeee   f   dededee	e
ef      fˆ fd„Zej                  ddfdej"                  d
e	e
ef   dedeee
ef      deee
ef      dej"                  fd„Z	 	 	 	 d-dej"                  d
e	e
ef   de
dedeee
ef      deee
ef      dej"                  fd„Z e«       ddddddddddddej,                  ddfd	ee   d
ee	e
ef      dee   dee   dee   dee
   dee   dee   dee   dee   dee   dee	e
ef      deee
ef      deee
ef      dee   fd„«       Zd.d ed!ede	e
ef   defd"„Zd.dd#dee	e
ef      dd#fd$„Z	 d.d%d#d&d#d'd#d(d#d)ed*ed+edee	e
ef      defd,„Zˆ xZS )/ÚFuyuImageProcessoraŠ	  
    This class should handle the image processing part before the main FuyuForCausalLM. In particular, it should
    handle:

    - Processing Images:
        Taking a batch of images as input. If the images are variable-sized, it resizes them based on the desired patch
        dimensions. The image output is always img_h, img_w of (1080, 1920)

        Then, it patches up these images using the patchify_image function.

    - Creating Image Input IDs:
        For each patch, a placeholder ID is given to identify where these patches belong in a token sequence. For
        variable-sized images, each line of patches is terminated with a newline ID.

    - Image Patch Indices:
        For each image patch, the code maintains an index where these patches should be inserted in a token stream.


    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image to `size`.
        size (`Dict[str, int]`, *optional*, defaults to `{"height": 1080, "width": 1920}`):
            Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
            `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`.
        do_pad (`bool`, *optional*, defaults to `True`):
            Whether to pad the image to `size`.
        padding_value (`float`, *optional*, defaults to 1.0):
            The value to pad the image with.
        padding_mode (`str`, *optional*, defaults to `"constant"`):
            The padding mode to use when padding the image.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image.
        image_mean (`float`, *optional*, defaults to 0.5):
            The mean to use when normalizing the image.
        image_std (`float`, *optional*, defaults to 0.5):
            The standard deviation to use when normalizing the image.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image.
        rescale_factor (`float`, *optional*, defaults to `1 / 255`):
            The factor to use when rescaling the image.
        patch_size (`Dict[str, int]`, *optional*, defaults to `{"height": 30, "width": 30}`):
            Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
    ©r   Úimage_input_idsÚimage_patchesÚimage_patch_indices_per_batchÚ#image_patch_indices_per_subsequenceTNç      ð?Úconstantg      à?gp?Ú	do_resizeÚsizeÚresampleÚdo_padÚpadding_valueÚpadding_modeÚdo_normalizeÚ
image_meanÚ	image_stdÚ
do_rescaleÚrescale_factorÚ
patch_sizec                 óô   •— t        ‰| �  di |¤Ž || _        |�|ndddœ| _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        || _        |�|| _        y dddœ| _        y )Ni8  i€  )ÚheightÚwidthé   r2   )ÚsuperÚ__init__rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   )r>   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   rI   Ú	__class__s                 €r(   rt   zFuyuImageProcessor.__init__ì   sŠ   ø€ ô  	‰ÑÑ"˜6Ò"Ø"ˆŒØ Ð,‘D¸TÈDÑ2QˆŒ	Ø ˆŒØˆŒØ*ˆÔØ(ˆÔØ(ˆÔØ$ˆŒØ"ˆŒØ$ˆŒØ,ˆÔØ(2Ð(>˜*ˆ�ÈrÐ\^ÑD_ˆ�r7   r'   Údata_formatÚinput_data_formatr    c           	      óÚ   — t        ||«      \  }}|d   |d   }
}	||
k  r||	k  r|S |	|z  }|
|z  }t        ||«      }t        ||z  «      }t        ||z  «      }t        d|||f|||dœ|¤Ž}|S )a�  
        Resize an image to `(size["height"], size["width"])`.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.

        Returns:
            `np.ndarray`: The resized image.
        rp   rq   )r'   rd   re   rv   rw   r2   )r   ÚminrN   r   )r>   r'   rd   re   rv   rw   rI   Úimage_heightÚimage_widthÚtarget_heightÚtarget_widthÚheight_scale_factorÚwidth_scale_factorÚoptimal_scale_factorÚ
new_heightÚ	new_widthÚscaled_images                    r(   r   zFuyuImageProcessor.resize
  s¶   € ôF %3°5Ð:KÓ$LÑ!ˆ�kØ&*¨8¡n°d¸7±m�|ˆà˜,Ò&¨<¸=Ò+HØˆLà+¨lÑ:ÐØ)¨KÑ7ÐÜ"Ð#6Ð8JÓKÐä˜Ð(<Ñ<Ó=ˆ
Ü˜Ð&:Ñ:Ó;ˆ	äð 
ØØ˜iÐ(ØØ#Ø/ñ
ð ñ
ˆð Ðr7   ÚmodeÚconstant_valuesc                 ó‚   — t        ||«      \  }}|d   |d   }
}	d}d}|	|z
  }|
|z
  }t        |||f||ff||||¬«      }|S )aˆ  
        Pad an image to `(size["height"], size["width"])`.

        Args:
            image (`np.ndarray`):
                Image to pad.
            size (`Dict[str, int]`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
            data_format (`ChannelDimension` or `str`, *optional*):
                The data format of the output image. If unset, the same format as the input image is used.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        rp   rq   r   )Úpaddingr„   r…   rv   rw   )r   r   )r>   r'   rd   r„   r…   rv   rw   rz   r{   r|   r}   Úpadding_topÚpadding_leftÚpadding_bottomÚpadding_rightÚpadded_images                   r(   Ú	pad_imagezFuyuImageProcessor.pad_imageD  sx   € ô, %3°5Ð:KÓ$LÑ!ˆ�kØ&*¨8¡n°d¸7±m�|ˆØˆØˆØ&¨Ñ5ˆØ$ {Ñ2ˆÜØØ! >Ð2°\À=Ð4QÐRØØ+Ø#Ø/ô
ˆð Ðr7   Úreturn_tensorsc                 ó  — |�|n| j                   }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }|	�|	n| j                  }	|
�|
n| j                  }
|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }t        |t        «      rt        d„ |D «       «      rt        d«      ‚t!        |«      }t#        ||||	|
|||||¬«
       |D ��cg c]  }|D �cg c]  }t%        |«      ‘Œ c}‘Œ }}}|r&t'        |d   d   «      rt(        j+                  d«       |€t-        |d   d   «      }|D �cg c]  }t/        |d   |¬«      ‘Œ }}t1        |«      }|r1|D ��cg c]$  }|D �cg c]  }| j3                  |||¬«      ‘Œ c}‘Œ& }}}|D �cg c]  }t/        |d   |¬«      ‘Œ }}|D �cg c]  }|d   g‘Œ
 }}|D �cg c]  }|d   g‘Œ
 }}t5        ||«      D ��cg c]  \  }}|d   |d   z  g‘Œ }}}|r3|D ��cg c]&  }|D �cg c]  }| j7                  |||||¬	«      ‘Œ c}‘Œ( }}}|r1|D ��cg c]$  }|D �cg c]  }| j9                  |||¬
«      ‘Œ c}‘Œ& }}}|r2|D ��cg c]%  }|D �cg c]  }| j;                  ||	|
|¬«      ‘Œ c}‘Œ' }}}|�*|D ��cg c]  }|D �cg c]  }t=        |||«      ‘Œ c}‘Œ }}}||||dœ}t?        ||¬«      S c c}w c c}}w c c}w c c}w c c}}w c c}w c c}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w )aô  

        Utility function to preprocess the images and extract necessary information about original formats.

        Args:
            images (`ImageInput`):
                Images to preprocess. Expects a single image, a list or images or a list of lists of images. Pixel
                values range from 0 to 255, or between 0 and 1 if `do_rescale` is `False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image to `size`.
            size (`Dict[str, int]`, *optional*, defaults to `self.size`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `self.resample`):
                `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`.
            do_pad (`bool`, *optional*, defaults to `self.do_pad`):
                Whether to pad the image to `size`.
            padding_value (`float`, *optional*, defaults to `self.padding_value`):
                The value to pad the image with.
            padding_mode (`str`, *optional*, defaults to `self.padding_mode`):
                The padding mode to use when padding the image.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float`, *optional*, defaults to `self.image_mean`):
                The mean to use when normalizing the image.
            image_std (`float`, *optional*, defaults to `self.image_std`):
                The standard deviation to use when normalizing the image.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                The factor to use when rescaling the image.
            patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format of the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        c              3   ó\   K  — | ]$  }t        |t        «      xr t        |«      d k\  –— Œ& y­w)é   N)r$   r%   rL   )r&   r3   s     r(   r)   z0FuyuImageProcessor.preprocess.<locals>.<genexpr>¿  s)   è ø€ Ò+iÐZ^¬J°t¼TÓ,BÒ,UÄsÈ4ÃyÐTUÁ~Ó,UÑ+iùs   ‚*,z:Multiple images for a single sample are not yet supported.)
rl   rm   ri   rj   rk   rf   Úsize_divisibilityrc   rd   re   r   z­It looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.)Úchannel_dim)rd   rw   é   )rd   r„   r…   rw   )Úscalerw   )ÚmeanÚstdrw   )r   Úimage_unpadded_heightsÚimage_unpadded_widthsÚimage_scale_factors)rP   r/   ) rc   rd   re   rf   rl   rm   ri   rj   rk   rg   rh   rn   r$   r%   Úanyr+   r,   r   r   r   ÚloggerÚwarning_oncer   r   r
   r   Úzipr�   ÚrescaleÚ	normalizer   r.   )r>   r   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   rv   rw   rŽ   Úbatch_imagesr'   Úoriginal_image_sizesÚimage_sizesÚ
image_sizer˜   r™   Úoriginal_sizeÚresized_sizerš   rP   s                               r(   Ú
preprocesszFuyuImageProcessor.preprocessj  sL  € ðL "+Ð!6‘I¸D¿N¹Nˆ	ØÐ'‰t¨T¯Y©YˆØ'Ð3‘8¸¿¹ˆØ!Ð-‘°4·;±;ˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø+9Ð+E™È4×K^ÑK^ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø!*Ð!6‘I¸D¿N¹Nˆ	Ø)6Ð)B™È×HZÑHZˆØ'3Ð'?‘|ÀT×EVÑEVˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø+9Ð+E™È4×K^ÑK^ˆØ#-Ð#9‘Z¸t¿¹ˆ
ä�fœdÔ#¬Ñ+iÐbhÔ+iÔ(iÜÐYÓZÐZä2°6Ó:ˆä%Ø!Ø)Ø%Ø!ØØØ"ØØØõ	
ð S_×_È¸FÖC°5œ¨Õ.ÔCÐ_ˆÑ_áœ/¨,°q©/¸!Ñ*<Ô=Ü×Ñðsôð
 Ð$ä >¸|ÈA¹ÈqÑ?QÓ RÐàgsÖtÐ]c¤¨v°a©yÐFWÖ XÐtÐÐtÜ˜TÓ"ˆáð +÷àð bhÖhÐX]�—‘˜U¨ÐAR�ÕSÔhðˆLñ ð
 _kÖkÐTZ”~ f¨Q¡iÐ=NÖOÐkˆÐkØDOÖ!P°j :¨a¡=¢/Ð!PÐÐ!PØCNÖ O°Z *¨Q¡-¢Ð OÐÐ Oô
 03Ð3GÈÓ/U÷
á+�˜|ð ˜!‰_˜}¨QÑ/Ñ/Ò0ð
Ðñ 
ñ
 ð +÷ð ð "(ö	ð ð —N‘NØØ!Ø)Ø(5Ø*;ð #õ ô	ðˆLñ ñ ð +÷àð ntÖtÐdi�—‘˜e¨>ÐM^�Õ_ÔtðˆLñ ñ
 ð +÷ð
 ð "(öàð —N‘N 5¨z¸yÐ\m�NÕnôðˆLñ ð Ð"ð +÷àð bhÖhÐX]Ô,¨U°KÐARÕSÔhðˆLñ ð #Ø&<Ø%:Ø#6ñ	
ˆô   T°~ÔFÐFùòS DùÓ_ùò  uùò
 iùóùò
 lùÚ!PùÚ Oùó
ùò	ùóùò  uùóùòùóùò iùós®   Ä3	L8Ä<L3ÅL8ÆL>Ç 	MÇ	MÇ$MÇ0MÈMÈ MÈ>MÉ	M(É'M#ÊM(Ê	M3ÊM.Ê7M3Ë	M>ËM9Ë+M>Ë:	N	ÌNÌN	Ì3L8ÍMÍ#M(Í.M3Í9M>ÎN	rz   r{   c                 óä   — |�|n| j                   }| j                   d   | j                   d   }}||z  dk7  rt        d|›d|› �«      ‚||z  dk7  rt        d|›d|› �«      ‚||z  }||z  }||z  }|S )a¨  
        Calculate number of patches required to encode an image.

        Args:
            image_height (`int`):
                Height of the image.
            image_width (`int`):
                Width of the image.
            patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
        rp   rq   r   zimage_height=z must be divisible by zimage_width=)rn   r+   )	r>   rz   r{   rn   Úpatch_heightÚpatch_widthÚnum_patches_per_dim_hÚnum_patches_per_dim_wÚnum_patchess	            r(   Úget_num_patchesz"FuyuImageProcessor.get_num_patches  s¢   € ð $.Ð#9‘Z¸t¿¹ˆ
Ø$(§O¡O°HÑ$=¸t¿¹ÈwÑ?W�kˆà˜,Ñ&¨!Ò+Ü  ˜Ð.DÀ\ÀNÐSÓTÐTØ˜Ñ$¨Ò)Ü  ˜~Ð-CÀKÀ=ÐQÓRÐRà ,°Ñ <ÐØ +¨{Ñ :ÐØ+Ð.CÑCˆØÐr7   ztorch.Tensorc                 óh  — t        | dg«       |�|n| j                  }|d   |d   }}|j                  \  }}}}|j                  d||«      }|j                  d||«      }	|	j	                  «       }	|	j                  ||d||«      }	|	j                  ddddd	«      }	|	j                  |d||z  |z  «      }	|	S )
a|  
        Convert an image into a tensor of patches.

        Args:
            image (`torch.Tensor`):
                Image to convert. Shape: [batch, channels, height, width]
            patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
        rC   rp   rq   r‘   r   éÿÿÿÿr   é   r”   )r   rn   ÚshapeÚunfoldÚ
contiguousÚviewÚpermuteÚreshape)
r>   r'   rn   r©   rª   Ú
batch_sizeÚchannelsÚ_Úunfolded_along_heightÚpatchess
             r(   Úpatchify_imagez!FuyuImageProcessor.patchify_image5  sÉ   € ô 	˜$  	Ô*Ø#-Ð#9‘Z¸t¿¹ˆ
Ø$.¨xÑ$8¸*ÀWÑ:M�kˆð
 &+§[¡[Ñ"ˆ
�H˜a Ø %§¡¨Q°¸lÓ KÐØ'×.Ñ.¨q°+¸{ÓKˆØ×$Ñ$Ó&ˆØ—,‘,˜z¨8°R¸À{ÓSˆØ—/‘/ ! Q¨¨1¨aÓ0ˆØ—/‘/ *¨b°(¸\Ñ2IÈKÑ2WÓXˆØˆr7   Úimage_inputÚimage_presentÚimage_unpadded_hÚimage_unpadded_wÚimage_placeholder_idÚimage_newline_idÚvariable_sizedc	           
      óŽ  — t        | dg«       |�|n| j                  }|d   |d   }
}	g }g }g }t        |j                  d   «      D �]\  }g }g }t        |j                  d   «      D �]  }|||f   �rÍ|||f   }|j                  d   |j                  d   }}|rft	        |t        j                  |||f   |	z  «      |	z  «      }t	        |t        j                  |||f   |
z  «      |
z  «      }|dd…d|…d|…f   }||}}| j                  ||¬«      }t        j                  |g|t        j                  |j                  ¬	«      }| j                  |j                  d«      ¬
«      j                  d«      }||j                  d   k(  sJ ‚|r|j                  d||
z  «      }t        j                  |j                  d   dg|t        j                  |j                  ¬	«      }t        j                   ||gd¬«      }|j                  d«      }|j#                  |g«       |j#                  |«       |j#                  |«       �ŒÙ|j#                  t        j$                  g t        j                  |j                  ¬	«      «       �Œ |j#                  |«       |j#                  |«       �Œ_ g }g }|D �]  }d}g }g } |D ]Õ  }!|!|k(  }"t        j&                  |"«      }t        j(                  |t        j*                  |!j                  ¬	«      j-                  |!«      }#t        j.                  |!d«      }$t        j.                  |!d«      }%t        j0                  |"d¬«      d   }&|#|z   |$|&<   |#|%|&<   |j#                  |$«       | j#                  |%«       ||z  }Œ× |j#                  |«       |j#                  | «       �Œ t3        |||||dœ¬«      S )a°  Process images for model input. In particular, variable-sized images are handled here.

        Args:
            image_input (`torch.Tensor` of shape [batch_size, subsequence_size, num_channels, height, width]):
                Tensor of images padded to model input size.
            image_present (`torch.Tensor` of shape [batch_size, subsequence_size, num_images]):
                Tensor of 1s and 0s indicating whether an image is present.
            image_unpadded_h (`torch.Tensor` of shape [batch_size, subsequence_size]):
                Tensor of unpadded image heights.
            image_unpadded_w (`torch.Tensor` of shape [batch_size, subsequence_size]):
                Tensor of unpadded image widths.
            image_placeholder_id (int):
                The id of the image placeholder token. Comes from an associated tokenizer.
            image_newline_id (int):
                The id of the image newline token. Comes from an associated tokenizer.
            variable_sized (bool):
                Whether to process images as variable-sized.
            patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`):
                Size of the patches.
        rC   Nrp   rq   r   r”   r‘   )rz   r{   )ÚdtyperD   )r'   r°   )ÚdimT)Úas_tupler\   )rP   )r   rn   Úranger²   ry   ÚmathÚceilr®   rC   ÚfullÚint32rD   r½   Ú	unsqueezeÚsqueezer·   ÚcatrO   ÚtensorÚcount_nonzeroÚarangeÚint64Útype_asÚ	full_likeÚnonzeror.   )'r>   r¾   r¿   rÀ   rÁ   rÂ   rÃ   rÄ   rn   r©   rª   r   Úbatch_image_patchesÚbatch_image_input_idsÚbatch_indexr]   r^   Úsubseq_indexr'   rz   r{   Únew_hÚnew_wr­   Útensor_of_image_idsr¼   Únewline_idsr_   r`   Úsample_image_input_idsÚindex_offsetÚper_batch_indicesÚper_subsequence_indicesÚsubseq_image_input_idsÚpatches_maskÚindicesÚindices_in_stream_per_batchÚ!indices_in_stream_per_subsequenceÚpatches_indss'                                          r(   Úpreprocess_with_tokenizer_infoz1FuyuImageProcessor.preprocess_with_tokenizer_infoO  s  € ô> 	˜$  	Ô*à#-Ð#9‘Z¸t¿¹ˆ
Ø$.¨xÑ$8¸*ÀWÑ:M�kˆð ,.ˆØ8:Ðà:<ÐÜ  ×!2Ñ!2°1Ñ!5Ó6ó /	6ˆKØ ˆOØˆMÜ % k×&7Ñ&7¸Ñ&:Ó ;ó )k�Ø  ¨lÐ!:Ó;Ø'¨°\Ð(AÑB�EØ05·±¸A±ÀÇÁÈAÁ +�LÙ%ô !$Ø(Ü ŸI™IÐ&6°{ÀLÐ7PÑ&QÐT`Ñ&`ÓaÐdpÑpó!˜ô !$Ø'Ü ŸI™IÐ&6°{ÀLÐ7PÑ&QÐT_Ñ&_Ó`ÐcnÑnó!˜ð !&¢a¨¨%¨°°%°Ð&7Ñ 8˜Ø49¸5 k˜à"&×"6Ñ"6ÀLÐ^iÐ"6Ó"j�KÜ*/¯*©*Ø$˜Ð';Ä5Ç;Á;ÐWb×WiÑWiô+Ð'ð #×1Ñ1¸¿¹ÈÓ8JÐ1ÓK×SÑSÐTUÓV�GØ&¨'¯-©-¸Ñ*:Ò:Ð:Ð:á%à.A×.IÑ.IÈ"ÈkÐ]hÑNhÓ.iÐ+Ü&+§j¡jØ0×6Ñ6°qÑ9¸1Ð=Ø,Ü"'§+¡+Ø#.×#5Ñ#5ô	'˜ô /4¯i©iÐ9LÈkÐ8ZÐ`aÔ.bÐ+Ø.A×.IÑ.IÈ"Ó.MÐ+à—M‘M 5 'Ô*Ø#×*Ñ*Ð+>Ô?Ø!×(Ñ(¨Ö1à#×*Ñ*¬5¯<©<¸Ä%Ç+Á+ÐVa×VhÑVhÔ+iÖjðS)kðV "×(Ñ(¨Ô9Ø×&Ñ& }Ö5ð_/	6ðf CEÐ%ØHJÐ+à&;ó 	PÐ"ØˆLØ "ÐØ&(Ð#Ø*@ò ,Ð&à5Ð9MÑM�Ü#×1Ñ1°,Ó?�ÜŸ,™, {¼%¿+¹+ÐNd×NkÑNkÔl×tÑtØ*ó�ô
 /4¯o©oÐ>TÐVXÓ.YÐ+Ü49·O±OÐDZÐ\^Ó4_Ð1Ü$Ÿ}™}¨\ÀDÔIÈ!ÑL�à<CÀlÑ<RÐ+¨LÑ9ØBIÐ1°,Ñ?à!×(Ñ(Ð)DÔEØ'×.Ñ.Ð/PÔQØ Ñ+‘ð%,ð( *×0Ñ0Ð1BÔCØ/×6Ñ6Ð7NÖOð3	Pô6  à Ø#8Ø!4Ø1NØ7Zñô
ð 	
r7   )rb   ra   NNr#   )rV   rW   rX   rY   Úmodel_input_namesr   ÚBILINEARÚboolr   r   rM   rN   Úfloatr   r   rt   ÚnpÚndarrayr   r   r�   r   ÚFIRSTr   r§   r®   r½   r.   rê   Ú__classcell__)ru   s   @r(   r[   r[   ¶   sC  ø„ ñ+òZÐð Ø)-Ø'9×'BÑ'BØØ"Ø&Ø!Ø03Ø/2ØØ 'Ø/3ñ`àð`ð �t˜C ˜H‘~Ñ&ð`ð %ð	`ð
 ð`ð ð`ð ð`ð ð`ð ˜%  e¡Ð,Ñ-ð`ð ˜  U¡Ð+Ñ,ð`ð ð`ð ð`ð ˜T # s (™^Ñ,õ`ðD (:×'BÑ'BØ>BØDHñ8à�z‰zð8ð �3˜�8‰nð8ð %ð	8ð
 ˜e CÐ)9Ð$9Ñ:Ñ;ð8ð $ E¨#Ð/?Ð*?Ñ$@ÑAð8ð 
�‰ó8ð| Ø!$Ø>BØDHñ$à�z‰zð$ð �3˜�8‰nð$ð ð	$ð
 ð$ð ˜e CÐ)9Ð$9Ñ:Ñ;ð$ð $ E¨#Ð/?Ð*?Ñ$@ÑAð$ð 
�‰ó$ñL %Ó&ð %)Ø)-Ø15Ø!%Ø)-Ø&*Ø'+Ø&*Ø%)Ø%)Ø*.Ø/3Ø>N×>TÑ>TØDHØ/3ñ#oGð ˜D‘>ðoGð �t˜C ˜H‘~Ñ&ð	oGð
 Ð-Ñ.ðoGð ˜‘ðoGð   ‘ðoGð ˜s‘mðoGð ˜t‘nðoGð ˜U‘OðoGð ˜E‘?ðoGð ˜T‘NðoGð ! ™ðoGð ˜T # s (™^Ñ,ðoGð ˜e CÐ)9Ð$9Ñ:Ñ;ðoGð  $ E¨#Ð/?Ð*?Ñ$@ÑAð!oGð" ! Ñ,ò#oGó 'ðoGñb¨Cð ¸cð ÈtÐTWÐY\ÐT\É~ð Ðiló ñ2 Nð ÀÈÈcÐSVÈhÉÑ@Xð Ðdró ðF 04ñB
à#ðB
ð &ðB
ð )ð	B
ð
 )ðB
ð "ðB
ð ðB
ð ðB
ð ˜T # s (™^Ñ,ðB
ð 
÷B
r7   r[   ),rY   rÊ   Útypingr   r   r   r   Únumpyrï   Úimage_processing_utilsr   r	   r
   Úimage_transformsr   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   r   r   r   rC   Ú
get_loggerrV   rœ   r,   r.   r[   Ú__all__r2   r7   r(   ú<module>rû      sÉ   ðñ &ã ß .Ó .ã ç UÑ U÷ñ ÷
÷ ÷ ÷÷ ñ ñ ÔÛð 
ˆ×	Ñ	˜HÓ	%€ðaØ�$�t˜JÑ'Ñ(¨$¨zÑ*:¸JÐFÑGðaà	ˆ$ˆzÑ
Ñóaôj�|ô jôZ[
Ð+ô [
ð|  Ð
 �r7   