
    ^jeI                     2   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mZmZ  ej8                  e      Zdeee      ee   z  ez  dee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 Fuyu.    N)
functional   )TorchvisionBackend)BatchFeatureget_size_dict)group_images_by_shapereorder_images)
ImageInputPILImageResamplingSizeDictis_valid_imagemake_list_of_images)ImagesKwargsUnpack)
TensorTypeauto_docstring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     y/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/fuyu/image_processing_fuyu.py	<genexpr>z.make_list_of_list_of_images.<locals>.<genexpr>1   s     'TE
5$(?'Ts   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'TV'T$T&$8>?u#E*??
_
`` @s   A(c                   6    e Zd ZU dZedz  ed<   eed<   eed<   y)FuyuImagesKwargsa  
    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.
    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.
    N
patch_sizepadding_valuepadding_mode)__name__
__module____qualname____doc__r   __annotations__floatstr     r   r$   r$   :   s     4r0   r$   F)totalc                       e Zd ZdZdddZdddZej                  ZdZ	dZ
dZdZdZdZdZd	Zg d
ZeZdee   f fdZ	 d3dededefdZ	 	 d4dej4                  dedddedej4                  f
 fdZded   dededd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dz  d!e dz  d"edz  d#e e!z  dz  de"fd$Z#d5d%ed&ed'edz  defd(Z$d5dej4                  d'edz  dej4                  fd)Z%	 d5d*ej4                  d+ej4                  d,ej4                  d-ej4                  d.ed/ed0ed'e&e ef   dz  de"fd1Z'	 d5d'e&e ef   ez  dz  de&f fd2Z( xZ)S )6FuyuImageProcessorTi8  i  heightwidth   g      ?constantg      ?gp?r   image_input_idsimage_patchesimage_patch_indices_per_batch#image_patch_indices_per_subsequencekwargsc                 $    t        |   di | y )Nr/   )super__init__)selfr>   	__class__s     r   rA   zFuyuImageProcessor.__init__`   s    "6"r0   r   expected_ndimsr   c                 :    | j                  |      }t        |      S r   )fetch_imagesr"   )rB   r   rD   s      r   _prepare_images_structurez,FuyuImageProcessor._prepare_images_structurec   s    
 ""6**622r0   Nr   sizeresamplez7PILImageResampling | tvF.InterpolationMode | int | None	antialiasc                 4   |t         j                  }|j                  dd \  }}|j                  |j                  }	}||	k  r||k  r|S ||z  }
|	|z  }t        |
|      }t        ||z        }t        ||z        }t        | !  |t        ||      ||      S )a  
        Resize an image to fit within `(size.height, size.width)` while maintaining aspect ratio.
        Only resizes if the image is larger than the target size.
        Args:
            image (`torch.Tensor`):
                Image to resize.
            size (`SizeDict`):
                Dictionary in the format `{"height": int, "width": int}` specifying the max size of the output image.
            resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                Resampling filter to use when resizing the image.
            antialias (`bool`, *optional*, defaults to `True`):
                Whether to apply antialiasing when resizing.
        Nr4   )rI   rJ   )
r   BILINEARshaper5   r6   minintr@   resizer   )rB   r   rH   rI   rJ   r>   image_heightimage_widthtarget_heighttarget_widthheight_scale_factorwidth_scale_factoroptimal_scale_factor
new_height	new_widthrC   s                  r   rQ   zFuyuImageProcessor.resizek   s    * )22H$)KK$4!k&*kk4::|,&<=+HL+l:)K7"#68JK(<<=
&::;	w~8:Y?(^g  
 	
r0   ztorch.Tensor	do_resize
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padr&   r'   disable_groupingreturn_tensorsc           	         |D cg c]  }|s|d   j                   dd   }}t        ||d      \  }}i }|j                         D ]   \  }}|r| j                  |||      }|||<   " t	        ||d      }|D cg c]  }|s|d   j                   dd   }}|D cg c]  }|d   g
 }}|D cg c]  }|d   g
 }}t        ||      D cg c]  \  }}|d   |d   z  g }}}|
r| j                  |||||d      }t        ||d      \  }}i }|j                         D ]   \  }}| j                  ||||||	      }|||<   " t	        ||d      } t        j                  | D !cg c]  }!|!st        j                  |!       c}!      }"t        |"|||d	|d
g      S c c}w c c}w c c}w c c}w c c}}w c c}!w )Nr   rL   T)rb   	is_nested)r   rH   rI   )re      )pad_size
fill_valuer'   rb   re   )r   image_unpadded_heightsimage_unpadded_widthsimage_scale_factorsoverflowing_values)datatensor_typeskip_tensor_conversion)rN   r   itemsrQ   r	   zippadrescale_and_normalizetorchstackr   )#rB   r   r[   rH   rI   r\   r]   r^   r_   r`   ra   r&   r'   rb   rc   r>   batch_imageoriginal_image_sizesgrouped_imagesgrouped_images_indexresized_images_groupedrN   stacked_imagesresized_imagesimage_sizes
image_sizeri   rj   original_sizeresized_sizerk   processed_images_groupedprocessed_imagesbatchimages_tensors#                                      r   _preprocesszFuyuImageProcessor._preprocess   sO   & NTckWbA 4 4RS 9cc/D%50
,, "$%3%9%9%; 	;!E>!%>W_!`,:"5)	; ((>@T`deDRb[Va{1~++BC0bbDO!Pj:a=/!P!PCN OZ*Q- O O 033G/U
+| !_}Q//0
 
 !XX()!1 & N 0E-=0
,, $& %3%9%9%; 	=!E>!77
NL*V_N /=$U+	= **BDXdhiEU$_EY^U[[%7$_`'*@)>':	 '$8#9	
 		
U  d c!P O
6 %`s4   F!F!;F&F&F+0F0F5'F;/F;rR   rS   r%   c                 4   |<t        | j                  t              r| j                  }nt        di | j                  }|j                  |j                  }}||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 (`SizeDict`, *optional*):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
        r   zimage_height=z must be divisible by zimage_width=r/   )r   r%   r   r5   r6   r!   )	rB   rR   rS   r%   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     $//84!__
%88
$.$5$5z7G7Gk,&!+.D\NSTT$)~-CK=QRR , < +{ :+.CCr0   c                    t        | dg       |<t        | j                  t              r| j                  }nt        di | j                  }|j                  |j
                  }}|j                  \  }}}}|j                  d||      }|j                  d||      }	|	j                         }	|	j                  ||d||      }	|	j                  ddddd      }	|	j                  |d||z  |z        }	|	S )	aw  
        Convert an image into a tensor of patches using PyTorch's unfold operation.
        Args:
            image (`torch.Tensor`):
                Image to convert. Shape: [batch, channels, height, width]
            patch_size (`SizeDict`, *optional*):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches.
        rt      r   r      rf   r/   )r   r   r%   r   r5   r6   rN   unfold
contiguousviewpermutereshape)
rB   r   r%   r   r   
batch_sizechannels_unfolded_along_heightpatchess
             r   patchify_imagez!FuyuImageProcessor.patchify_image   s     	$	*$//84!__
%88
$.$5$5z7G7Gk%*[["
Ha %Ql K'..q+{K$$&,,z8R{S//!Q1a0//*b(\2IK2WXr0   image_inputimage_presentimage_unpadded_himage_unpadded_wimage_placeholder_idimage_newline_idvariable_sizedc	           
      B   t        | dg       |=t        | j                  t              r| j                  }n1t        di | j                  }nt        |t              st        di |}|j                  |j
                  }
}	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+                  |       a g }g }|D ]  }d}g }g } |D ]  }!|!|k(  }"t        j.                  |"      }t        j0                  |t        j2                  |!j                        j5                  |!      }#t        j6                  |!d	      }$t        j6                  |!d	      }%t        j8                  |"d      d   }&|#|z   |$|&<   |#|%|&<   |j+                  |$       | j+                  |%       ||z  } |j+                  |       |j+                  |         t;        |||||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*):
                Size of the patches.
        rt   Nr   rf   r   )rR   rS   r%   )dtypedevice)r   r%   r   )dimT)as_tupler9   )rm   r/   )r   r   r%   r   r5   r6   rangerN   rO   mathceilr   rt   fullint32r   r   	unsqueezesqueezer   catappendtensorcount_nonzeroarangeint64type_as	full_likenonzeror   )'rB   r   r   r   r   r   r   r   r%   r   r   r   batch_image_patchesbatch_image_input_idsbatch_indexr:   r;   subseq_indexr   rR   rS   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_info  sI   @ 	$	*$//84!__
%88
J1!/J/J$.$5$5z7G7Gk+-8::< !2!21!56 /	6K OM %k&7&7&: ; *k l!:;'\(ABE05AA+L% !$( II&6{L7P&QT`&`adpp! !$' II&6{L7P&QT_&_`cnn! !&a%%&7 8495k"&"6"6%1{Wa #7 #K +0**$';5;;WbWiWi+' #118JWa1bjjklmG&'--*::::%.A.I.I"k]hNh.i+&+jj066q91=,"'++#.#5#5	' /4ii9Lk8Z`a.b+.A.I.I".M+MM5'*#**+>?!((1#**5<<%++VaVhVh+ijU*kV "((9&&}5_/	6b CE%HJ+&; 	P"L "&(#*@ ,&59MM#11,?,,{%++NdNkNkltt* /4oo>TVX.Y+49OODZ\^4_1$}}\DI!L<Cl<R+L9BI1,?!(()DE'../PQ+#,& *001BC/667NO1	P2  #8!41N7Z
 	
r0   c                     t        |   di |}|&t        |t              st        di t	        |d      }||d<   |S )zA
        Process Fuyu-specific kwargs before validation.
        r%   )
param_namer/   )r@   _standardize_kwargsr   r   r   )rB   r%   r>   rC   s      r   r   z&FuyuImageProcessor._standardize_kwargs  sI     ,6v6!*Z*J!WM*$VWJ)|r0   )r   )NTr   )*r(   r)   r*   r[   rH   r%   r   rM   rI   ra   r&   r'   r^   r_   r`   r\   r]   model_input_namesr$   valid_kwargsr   rA   r
   rP   rG   rt   Tensorr   boolrQ   r   r-   r.   r   r   r   r   r   dictr   r   __classcell__)rC   s   @r   r3   r3   I   s   IT*D,J!**HFMLLJIJN $L#(8!9 #  33 3 
	3 OS&
||&
 &
 L	&

 &
 
&
PF
^$F
 F
 	F

 LF
 F
 F
 F
 DK'$.F
 4;&-F
 tF
 t|F
 DjF
 +F
 j(4/F
" 
#F
PC c xZ^ jm 4ELL ho Y^YeYe H -1D
\\D
 ||D
  ,,	D

  ,,D
 "D
 D
 D
 cNT)D
 
D
P 8<cNX-4 
	 r0   r3   )$r+   r   rt   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   
get_loggerr(   loggerr   r"   r$   r3   __all__r/   r0   r   <module>r      s    &   7 ; A E  5  
		H	%aj!"T*%55
Ba	$z
a|5  X+ X Xv
  
 r0   