
    <h1                         S SK JrJr  S SKrSSKJr  SSKJr  SSK	J
r
JrJrJrJrJr  SSKJrJr  SSKJr  \" 5       (       a  S	S
KJr   " S S\SS9r " S S\
SS9r " S S\SS9r " S S\5      rS/rg)    )OptionalUnionN   )BatchFeature)
ImageInput)ImagesKwargsMultiModalDataProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInput)is_vision_available   )smart_resizec                        \ rS rSr% \\S'   Srg)Emu3TextKwargs    return_for_image_generation N)__name__
__module____qualname____firstlineno__bool__annotations____static_attributes__r       `/var/www/html/shao/venv/lib/python3.13/site-packages/transformers/models/emu3/processing_emu3.pyr   r       s    !%%r   r   F)totalc                   *    \ rS rSr% \\S'   \\S'   Srg)Emu3ImagesKwargs$   ratio
image_arear   N)r   r   r   r   strr   intr   r   r   r    r#   r#   $   s    JOr   r#   c                   @    \ rS rSr% \\S'   \\S'   SSS.SSS.S	.rS
rg)Emu3ProcessorKwargs)   text_kwargsimages_kwargsF)r   return_mm_token_type_idsz1:1i  )r%   r&   )r,   r-   r   N)	r   r   r   r   r   r   r#   	_defaultsr   r   r   r    r*   r*   )   s/    ## ,1(-

  
	Ir   r*   c                      ^  \ rS rSrSrSS/rSrSr SU 4S jjr    SS\	\
   S	\	\\\\\   \\   4      S
\\   S\4S jjrSS jrS rS\
4S jrS rS r\S 5       rSrU =r$ )Emu3Processor8   az  
Constructs a Emu3 processor which wraps a Emu3 image processor and a GPT2 tokenizer into a single
processor.

[`Emu3Processor`] offers all the functionalities of [`Emu3ImageProcessor`] and [`GPT2TokenizerFast`].
See the [`~Emu3Processor.__call__`] and [`~Emu3Processor.decode`] for more information.

Args:
    image_processor ([`Emu3ImageProcessor`]):
        The image processor is a required input.
    tokenizer ([`Emu3TokenizerFast`]):
        The tokenizer is a required input.
    chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
        in a chat into a tokenizable string.
image_processor	tokenizer)GPT2TokenizerGPT2TokenizerFastEmu3ImageProcessorc                   > UR                   U l         UR                  U l        UR                  U l        UR                  U l        UR                  U l        UR                  U l        UR                  U l	        SU l
        [        TU ]1  XUS9  g )N   )chat_template)image_tokenimage_token_id	boi_tokenimage_start_token	eoi_tokenimage_end_tokenimage_wrapper_tokenfake_token_around_image	eof_token	bos_tokendownsample_ratiosuper__init__)selfr3   r4   r:   kwargs	__class__s        r    rG   Emu3Processor.__init__M   s     %00'66!*!4!4(22'0'D'D$",,",, !=Qr   imagestextrI   returnc                    [        U[        5      (       a  U/nO8[        U[        5      (       d#  [        US   [        5      (       d  [        S5      eU R                  " [
        4SU R                  R                  0UD6nUS   R                  SS5      nUS   R                  SS	5      nUS   R                  S
S	5      n	U(       a  Ub  [        S5      eU(       d  Uc  Uc  [        S5      e0 n
U R                   nU R                   U R                   3nU(       Gd  UGb  U R                  " U40 US   D6n
[        U
R                  5      n/ nU H  nU R                   U;   a  [#        U5      nUu  nnUU R$                  -  nUU R$                  -  nUUS-   -  nU U SU U R&                   SU-   U 3nUR)                  U R                   US5      nU R*                   U 3nU R                   U;   a  M  UR-                  U5        M     U Vs/ sH  oR)                  SU R                   5      PM      nnOoU(       ah  U R/                  XU R$                  5      u  nnU U SU U R&                   3nU Vs/ sH  oR*                   U U 3PM     nnUU//[1        U5      -  U
S'   US   R                  SS	5      nUS   R                  SS5      nU R                  " U40 US   DSS	0D6nU R3                  UUS/S9  U(       aW  [4        R6                  " US   5      n[4        R8                  " US   5      nSUUU R:                  :H  '   UR=                  5       US'   [?        0 UEU
EUS9$ s  snf s  snf )a  
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
and `kwargs` arguments to Emu3TokenizerFast's [`~Emu3TokenizerFast.__call__`] if `text` is not `None` to encode
the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
of the above two methods for more information.

Args:
    images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`):
        The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
        tensor. Both channels-first and channels-last formats are supported.
    text (`str`, `list[str]`, `list[list[str]]`):
        The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
        (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
        `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
    return_tensors (`str` or [`~utils.TensorType`], *optional*):
        If set, will return tensors of a particular framework. Acceptable values are:

        - `'tf'`: Return TensorFlow `tf.constant` objects.
        - `'pt'`: Return PyTorch `torch.Tensor` objects.
        - `'np'`: Return NumPy `np.ndarray` objects.
        - `'jax'`: Return JAX `jnp.ndarray` objects.

Returns:
    [`BatchFeature`]: A [`BatchFeature`] with the following fields:

    - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
    - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
      `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
      `None`).
    - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
r   zAInvalid input text. Please provide a string, or a list of stringstokenizer_init_kwargsr,   r   Fr-   r%   Nr&   zGYou should not provide `images` when `return_for_image_generation=True`zOYou must provide either text or images when `return_for_image_generation=False`r   *z<placeholder>image_sizesreturn_tensorsr.   image)
modalities	input_idsmm_token_type_ids)datatensor_type) 
isinstancer'   list	TypeError_merge_kwargsr*   r4   init_kwargspop
ValueErrorr>   rC   r@   r3   iterrR   r;   nextrE   rB   replacerD   appendcalculate_generate_sizelen_check_special_mm_tokensnparray
zeros_liker<   tolistr   )rH   rL   rM   audiovideosrI   output_kwargsr   r%   r&   image_featuresimage_start_tokensimage_end_tokensrR   prompt_stringssample
image_sizeheightwidthimage_seq_lengthimage_placeholderimage_promptrS   r.   text_inputs	array_idsrW   s                              r    __call__Emu3Processor.__call__^   s   T dC  6DD$''
47C0H0H_``**
"&.."<"<
 

 '4M&B&F&FGdfk&l#o.227DA"?377dK
&6+=fgg*t|noo $ 6 67"nn-d.B.B-CD +v/A!11&[M/<Z[N~99:KN&&&0!%k!2J$.MFE#t'<'<<F!T%:%::E'-';$+=*>vhawtOkOkNlm|  @P  nP  mQ  Rb  Qc  )d%#^^D,<,<>OQRSF $/x8F &&&0 %%f-  UccTb&NN?D4D4DETbDcD ) 88DLaLabMFE01&5'$B^B^A_`LLPQD&~~&vh|n=DDQ.4e_,=D	,IN=) '}599:JDQ#0#?#C#CD^`e#f nnT_]=-I_Z^_%%dKWI%N#[!9:I "k+.F GBCi4+>+>>?/@/G/G/IK+,!BK!B>!BP^__+ d Rs   $M$2M)c                    0 nUb  / nU H  u  pV[        UUU R                  R                  U R                  R                  U R                  R                  5      u  pVXPR
                  -  nX`R
                  -  nXVS-   -  nUR                  U5        M     S/[        U5      -  nUR                  XHS.5        [        S0 UD6$ )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.
r   )num_image_tokensnum_image_patchesr   )
r   r3   spatial_factor
min_pixels
max_pixelsrE   rd   rf   updater	   )	rH   rR   rI   vision_datar   ru   rv   rw   r   s	            r    _get_num_multimodal_tokens(Emu3Processor._get_num_multimodal_tokens   s     "!!, ,((77((33((33!  #8#88!6!66#)QY#7  ''(89 "- "#c+&6 64Dmn,,,r   c                     [        [        UR                  S5      5      u  pEXE-  nX&-  S-  n[        [        XW-  U-  5      5      n[        [        XG-  U-  5      5      n	X4$ )N:g      ?)mapr(   splitround)
rH   r%   r&   r   rv   ru   current_areatarget_ratiotoken_heighttoken_widths
             r    re   %Emu3Processor.calculate_generate_size   sd    CS!12~"1c95!6!GHI% 4~ EFG((r   c                 <    U R                   R                  " U40 UD6$ N)r3   postprocess)rH   rL   rI   s      r    r   Emu3Processor.postprocess   s    ##//A&AAr   c                 :    U R                   R                  " U0 UD6$ )z
This method forwards all its arguments to Emu3TokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
refer to the docstring of this method for more information.
)r4   batch_decoderH   argsrI   s      r    r   Emu3Processor.batch_decode   s    
 ~~**D;F;;r   c                 :    U R                   R                  " U0 UD6$ )z
This method forwards all its arguments to Emu3TokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
the docstring of this method for more information.
)r4   decoder   s      r    r   Emu3Processor.decode   s    
 ~~$$d5f55r   c                     U R                   R                  nU R                  R                  n[        [        R                  X-   5      5      $ r   )r4   model_input_namesr3   r[   dictfromkeys)rH   tokenizer_input_namesimage_processor_input_namess      r    r   Emu3Processor.model_input_names  s<     $ @ @&*&:&:&L&L#DMM"7"UVWWr   )rD   rE   rC   rB   r@   r>   r;   r<   r   )NNNN)r   r   r   r   __doc__
attributestokenizer_classimage_processor_classrG   r   r   r   r   r   r[   r   r*   r   r|   r   re   r   r   r   propertyr   r   __classcell__)rJ   s   @r    r1   r1   8   s      $[1J<O0 	R& (,hli`$i` uY(94	?DQbLccdei` ,-i` 
i`V -D)B* B<6 X Xr   r1   )typingr   r   numpyrh   image_processing_utilsr   image_utilsr   processing_utilsr   r	   r
   r   r   r   tokenization_utils_baser   r   utilsr   image_processing_emu3r   r   r#   r*   r1   __all__r   r   r    <module>r      ss   " #  2 % r r C ( 3&Zu &|5 
*% QXN QXh 
r   