
    ^j:*                        d dl Z d dlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ  e       rddlmZ  e
       rd dlZ e       rd d	lmZ n e       rd d	lmZ  ej4                  e      Z G d
 ded      ZdZdededededede eef   fdZ!dddeddfdZ"dddddede d   fdZ# e	de       G d de             Z$dgZ%y)     N   )BatchFeature)UnpackVideosKwargs)
TensorTypeadd_start_docstringsis_torch_availableis_torchvision_availableis_torchvision_v2_availableis_vision_availablelogging)BASE_VIDEO_PROCESSOR_DOCSTRINGBaseVideoProcessor)
VideoInput)PILImageResampling)
functionalc                   0    e Zd ZU dZeed<   eed<   eed<   y)Gemma4VideoProcessorKwargsa_  
    patch_size (`int`, *optional*):
        Size of each image patch in pixels.
    max_soft_tokens (`int`, *optional*):
        Maximum number of soft (vision) tokens per video frame.
        Must be one of {70, 140, 280, 560, 1120}.
    pooling_kernel_size (`int`, *optional*):
        Spatial pooling kernel size applied after patchification.
    
patch_sizemax_soft_tokenspooling_kernel_sizeN)__name__
__module____qualname____doc__int__annotations__     }/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/gemma4/video_processing_gemma4.pyr   r   0   s     Or   r   F)total)F      i  i0  i`  heightwidthr   max_patchesr   returnc                 p   | |z  }||dz  z  }t        j                  ||z        }|| z  }||z  }	||z  }
t        t        j                  ||
z              |
z  }t        t        j                  |	|
z              |
z  }|dk(  r|dk(  rt	        d||z   d      ||dz  z  |
z  }|dk(  r1|
}t        t        t        j                  || z              |
z  |      }n5|dk(  r0|
}t        t        t        j                  | |z              |
z  |      }||z  |kD  rt	        d|  d| d| d| d| d	|       ||fS )
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   zpAttempting to resize to a 0 x 0 image. Resized height should be divisible by `pooling_kernel_size * patch_size`=.z
Resizing [xz] to [z] but this exceeds z patches with patch_size )mathsqrtr   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                 r     get_aspect_ratio_preserving_sizer:   D   s    ~Hz1}-IYYy8+,FF?L5.K#j0I 

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

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

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

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

 ,&&r   videotorch.Tensorc                     | j                   \  }}}}||z  }||z  }| j                  ||||||      }|j                  dddddd      }|j                  |||z  d      }|S )z
    Convert 4D tensor video of shape (num_frames, num_channels, height, width) into 3D tensor of patches of shape
    (num_frames, num_patches_height * num_patches_width, patch_size * patch_size * num_channels).
    r   r)      r         )shapereshapepermute)	r;   r   
num_framesnum_channelsr$   r%   num_patches_heightnum_patches_widthpatched_videos	            r    convert_video_to_patchesrJ   z   s    
 /4kk+Jfe:-+MML"4jBSU_M "))!Q1a;M!))*6HK\6\^`aMr   	positionstarget_length)r<   r<   c                    | j                   d   }||z
  }|dkD  rjddd|ddg}ddd|ddf}t        j                  j                  j	                  | |dd      } t        j                  j                  j	                  ||dd      }| |fS )z6
    Pad the video along to max number of patches
    r@   r   constant)modevaluerA   )rB   torchnnr   pad)r;   rK   rL   current_lengthpadding_lengthpaddingpos_paddings          r    pad_to_max_patchesrX      s     [[^N"^3NaNAq1!Q15##''wZq'QHH''++I{[]+^	)r   zbConstructs a Gemma4 video processor that samples frames from videos for use with the Gemma4 model.c                       e Zd Zej                  Zg dZg dZdZdZ	dZ
dZdZdZdZdZdZdZdZeZd	d
gZdee   f fdZ fdZdej4                  dedededej:                  dej4                  fdZdedee   de f fdZ!	 	 	 d#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%e&z  dz  dedz  d!edz  dedz  de fd"Z' xZ(S )$Gemma4VideoProcessor)        r[   r[   )      ?r\   r\   NT       r"   r   pixel_values_videosvideo_position_idskwargsc                     t        |   di | | j                  t        vr t	        dt         d| j                   d      y )N!`max_soft_tokens` must be one of , got r*   r   )super__init__r   _SUPPORTED_SOFT_TOKENSr/   selfra   	__class__s     r    rf   zGemma4VideoProcessor.__init__   sN    "6"'==@AW@XX^_c_s_s^ttuvww >r   c                 .    d|d<   t        |   di | y )NF	do_resizer   )re   _validate_preprocess_kwargsrh   s     r    rm   z0Gemma4VideoProcessor._validate_preprocess_kwargs   s    
 ${+5f5r   r;   r   r&   r   resampler'   c                     |j                   d   |j                   d   }}t        |||||      \  }}	||k(  r|	|k(  r|S t        j                  |||	g|d      S )NrA   )r$   r%   r   r&   r   T)sizeinterpolation	antialias)rB   r:   tvFresize)
ri   r;   r   r&   r   rn   r$   r%   r7   r8   s
             r    aspect_ratio_preserving_resizez3Gemma4VideoProcessor.aspect_ratio_preserving_resize   ss     BR&F!# 3'
#| F"|u'<Lzz."	
 	
r   videosc                 $    t        |   |fi |S )N)re   
preprocess)ri   rw   ra   rj   s      r    ry   zGemma4VideoProcessor.preprocess   s    
 w!&3F33r   r<   do_convert_rgbrl   z"tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsr   c           	         |t         vrt        dt          d| d      ||dz  z  }g }g }g }d}|D ]s  }|r| j                  |      }|r| j                  |||||      }| j	                  ||||||	      }|j
                  d   }|j
                  d   |z  }|j
                  d	   |z  }t        ||      }|j                  |j
                  d   |dz  z         |j                  }t        j                  t        j                  ||
      t        j                  ||
      d      }t        j                  |d	      }|j                  |j
                  d   d      }|d   j                  |dd      }t        |||      \  }}|j                  |       |j                  |       v t        j                  |d      }t        j                  |d      }|||d}t!        ||
      S )Nrc   rd   r*   r)   r@   )r;   r   r&   r   rn   r   rp   rA   )devicexy)indexing)dim)N.)r_   r`   num_soft_tokens_per_video)datatensor_type)rg   r/   convert_to_rgbrv   rescale_and_normalizerB   rJ   appendr   rQ   meshgridarangestackrC   repeatrX   r   )ri   rw   rz   rl   rn   r{   r|   r}   r~   r   r   r   r   r   ra   r&   pixel_valuesposition_idsr   rE   r;   patch_heightpatch_widthpatchesr   
patch_gridstacked_gridreal_positionsrK   r   s                                 r    _preprocessz Gemma4VideoProcessor._preprocess   s   " "88@AW@XX^_n^oopqrr%(;Q(>>$&!
  	+E++E2;;) +(;% <  ..uj.R^`jluvEQJ ;;r?j8L++b/Z7K.ujAG%,,W]]1-=ATVWAW-WX\\F[8\&9J
 !;;zr:L)11'--2BAFN+I6==j!QON!3G^[!YGY(	*A 	+F {{<Q7{{<Q7 $0".)B

 >BBr   )NNN))r   r   r   r   BICUBICrn   r~   r   rq   default_to_squarerz   rl   r{   r}   rE   do_sample_framesr   r   r   r   valid_kwargsmodel_input_namesr   rf   rm   rQ   Tensorr   rt   InterpolationModerv   r   r   ry   listboolfloatstrr   r   __classcell__)rj   s   @r    rZ   rZ      s   
 "))H JIDNIJLJJO-L.0DEx(B!C x6
||
 
 	

 !
 ''
 

644 344 
	4& "&&**.FC^$FC FC 	FC
 7FC FC FC FC DK'$.FC 4;&-FC j(4/FC $JFC tFC !4ZFC  
!FCr   rZ   )&r,   rQ   image_processing_utilsr   processing_utilsr   r   utilsr   r   r	   r
   r   r   r   video_processing_utilsr   r   video_utilsr   image_utilsr   torchvision.transforms.v2r   rt   torchvision.transforms
get_loggerr   loggerr   rg   r   tupler:   rJ   rX   rZ   __all__r   r   r    <module>r      s>     2 4   Y % 1 ;8 
		H	%U   3 3'3'3' 3' 	3'
 3' 38_3'lN    &4EH
)*  h"HC- HC	HCV "
"r   