
    ^j@                         d Z ddlmZmZ ddlmZmZmZ ddlm	Z	m
Z
  e
j                  e      Z G d ded      Ze	 G d	 d
e             Zd
gZy)z
Processor class for Llava.
   )get_image_sizeto_numpy_array)MultiModalDataProcessingKwargsProcessorMixin)auto_docstringloggingc                       e Zd ZdddddiZy)LlavaProcessorKwargstext_kwargsF)paddingreturn_mm_token_type_idsreturn_text_replacement_offsetsN)__name__
__module____qualname__	_defaults     u/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/llava/processing_llava.pyr   r      s    5epuvIr   r   F)totalc                   N     e Zd ZeZ	 	 	 	 	 	 	 d fd	ZdededefdZ	ddZ
 xZS )	LlavaProcessorc                     || _         || _        || _        t        |d      r|j                  n|| _        |j                  | j                  d      d   | _        t        	| !  |||       y)a  
        patch_size (`int`, *optional*):
            Patch size from the vision tower.
        vision_feature_select_strategy (`str`, *optional*):
            The feature selection strategy used to select the vision feature from the vision backbone.
            Should be same as in model's config
        image_token (`str`, *optional*, defaults to `"<image>"`):
            Special token used to denote image location.
        num_additional_image_tokens (`int`, *optional*, defaults to 0):
            Number of additional tokens added to the image embeddings, such as CLS (+1). If the backbone has no CLS or other
            extra tokens appended, no need to set this arg.
        image_tokenF)add_special_tokens    )chat_templateN)	
patch_sizenum_additional_image_tokensvision_feature_select_strategyhasattrr   encodeimage_token_idsuper__init__)
selfimage_processor	tokenizerr   r!   r   r   r    kwargs	__class__s
            r   r&   zLlavaProcessor.__init__(   so    . %+F(.L+4;I}4U900[f'..t/?/?TY.Z[\])=Qr   image_inputs	image_idxreturnc                     |d   |   }t        t        |            \  }}|| j                  z  || j                  z  z  | j                  z   }| j                  dk(  r|dz  }| j
                  |z  S )Npixel_valuesdefault   )r   r   r   r    r!   r   )r'   r,   r-   r0   heightwidthnum_image_tokenss          r   replace_image_tokenz"LlavaProcessor.replace_image_tokenF   sx    #N3I>&~l'CD"doo5%4??:RSVZVvVvv..);!"222r   c                    i }|t         j                  j                  di       }|j                  |       |j                  dd      xs | j                  j
                  }|d   |d   }}|| j                  z  || j                  z  z  }|| j                  z  }| j                  dk(  r|dz  }|gt        |      z  }dgt        |      z  }	|j                  ||	d       t        d	i |S )
a  
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (height, width) per each image.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        Nimages_kwargs	crop_sizer3   r4   r1   r2   )r5   num_image_patchesr   )r   r   getupdater(   r9   r   r    r!   lenr   )
r'   image_sizesr*   vision_datar8   r9   resized_heightresized_widthr5   r:   s
             r   _get_num_multimodal_tokensz)LlavaProcessor._get_num_multimodal_tokensN   s     "0::>>PRSM  (%))+t<^@T@T@^@^I,5h,?7ASMN .$// AmW[WfWfFfg @ @@22i? A%  01C4DD!"c+&6 64D[lmn,,,r   )NNNNNz<image>r   )N)r   r   r   r   valid_processor_kwargsr&   dictintstrr6   rB   __classcell__)r+   s   @r   r   r   $   sE    1 '+$%R<3 3 3 3-r   r   N)__doc__image_utilsr   r   processing_utilsr   r   r   utilsr   r	   
get_loggerr   loggerr   r   __all__r   r   r   <module>rO      sj    : 
 - 
		H	%+5  F-^ F- F-R 
r   