+
    œ°³jP.  ã                   ór   € ^ RI HtHt ^RIHt ^RIHt  ! R R]RR7      tRt] ! R	 R
]4      4       t	R
.t
R# )é    )ÚProcessingKwargsÚProcessorMixin)Úmake_flat_list_of_images)Úauto_docstringc                   ó"   € ] tR t^tRRR//tRtR# )ÚLlama4ProcessorKwargsÚtext_kwargsÚpadding_sideÚleft© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú	_defaultsÚ__static_attributes__r   ó    Úm/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/models/llama4/processing_llama4.pyr   r      s   † àØ˜Fð
ð„Ir   r   F)Útotala>  {{- bos_token }}
{%- if custom_tools is defined %}
    {%- set tools = custom_tools %}
{%- endif %}
{%- if not tools_in_user_message is defined %}
    {%- set tools_in_user_message = true %}
{%- endif %}
{%- if not date_string is defined %}
    {%- if strftime_now is defined %}
        {%- set date_string = strftime_now("%d %b %Y") %}
    {%- else %}
        {%- set date_string = "26 Jul 2024" %}
    {%- endif %}
{%- endif %}
{%- if not tools is defined %}
    {%- set tools = none %}
{%- endif %}

{#- This block extracts the system message, so we can slot it into the right place. #}
{%- if messages[0]['role'] == 'system' %}    
    {%- if messages[0]['content'] is string %}
        {%- set system_message = messages[0]['content']|trim %}
    {%- else %}
        {#- FIXME: The processor requires an array, always. #}
        {%- set system_message = messages[0]['content'][0]['text']|trim %}
    {%- endif %}
    {%- set messages = messages[1:] %}
    {%- set user_supplied_system_message = true %}
{%- else %}
    {%- set system_message = "" %}
    {%- set user_supplied_system_message = false %}
{%- endif %}

{#- System message if the user supplied one #}
{%- if user_supplied_system_message %}
    {{- "<|header_start|>system<|header_end|>

" }}
    {%- if tools is not none %}
        {{- "Environment: ipython
" }}
    {%- endif %}
    {%- if tools is not none and not tools_in_user_message %}
        {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
        {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
        {{- "Do not use variables.

" }}
        {%- for t in tools %}
            {{- t | tojson(indent=4) }}
            {{- "

" }}
        {%- endfor %}
    {%- endif %}
    {{- system_message }}
    {{- "<|eot|>" }}
{%- endif %}

{#- Custom tools are passed in a user message with some extra guidance #}
{%- if tools_in_user_message and not tools is none %}
    {#- Extract the first user message so we can plug it in here #}
    {%- if messages | length != 0 %}
        {%- set first_user_message = messages[0]['content']|trim %}
        {%- set messages = messages[1:] %}
    {%- else %}
        {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
{%- endif %}
    {{- '<|header_start|>user<|header_end|>

' -}}
    {{- "Given the following functions, please respond with a JSON for a function call " }}
    {{- "with its proper arguments that best answers the given prompt.

" }}
    {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
    {{- "Do not use variables.

" }}
    {%- for t in tools %}
        {{- t | tojson(indent=4) }}
        {{- "

" }}
    {%- endfor %}
    {{- first_user_message + "<|eot|>"}}
{%- endif %}

{%- for message in messages %}
    {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
    {{- '<|header_start|>' + message['role'] + '<|header_end|>

' }}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- else %}
            {%- for content in message['content'] %}
                {%- if content['type'] == 'image' %}
                    {{- '<|image|>' }}
                {%- elif content['type'] == 'text' %}
                    {{- content['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- "<|eot|>" }}
    {%- elif 'tool_calls' in message and message.tool_calls|length > 0 %}
       {{- '<|header_start|>assistant<|header_end|>

' -}}
       {{- '<|python_start|>' }}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- else %}
            {%- for content in message['content'] %}
                {%- if content['type'] == 'image' %}
                    {{- '<|image|>' }}
                {%- elif content['type'] == 'text' %}
                    {{- content['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
       {{- '<|python_end|>' }}
        {%- for tool_call in message.tool_calls %}
           {{- '{"name": "' + tool_call.function.name + '", ' }}
           {{- '"parameters": ' }}
           {{- tool_call.function.arguments | tojson }}
           {{- "}" }}
        {%- endfor %}
       {{- "<|eot|>" }}
    {%- elif message.role == "tool" or message.role == "ipython" %}
        {{- "<|header_start|>ipython<|header_end|>

" }}
        {%- if message.content is mapping or message.content is iterable %}
            {{- message.content | tojson }}
        {%- else %}
            {{- message.content }}
        {%- endif %}
        {{- "<|eot|>" }}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|header_start|>assistant<|header_end|>

' }}
{%- endif %}
c                   ó²   a a€ ] tR t^!t o]tRR^RRRRRRRR]3V3R	 lV 3R
 llltR tRV 3R llt	RV 3R llt
V3R lR lt]V3R lR l4       tRtVtV ;t# )ÚLlama4ProcessorNg      à?ú	<|image|>ú<|image_start|>ú<|image_end|>ú	<|patch|>ú<|tile_x_separator|>ú<|tile_y_separator|>c                ó&   <€ V ^8„  d   QhRS[ RS[/# )é   Ú
patch_sizeÚpixel_shuffle_ratio)ÚintÚfloat)ÚformatÚ__classdict__s   "€r   Ú__annotate__ÚLlama4Processor.__annotate__%   s!   ø€ ÷ 78ñ 78ñ ð	78ñ
 #ñ78r   c                ó  <€ \         SV `  WVR7       \        \        RV^,          ,          4      4      V n        W0n        WPn        W`n        VP                  V P                  4      V n	        Wpn
        W€n        W�n        W n        W°n        R# )a®  
patch_size (`int`, *optional*, defaults to 28):
    The size of image patches for tokenization.
pixel_shuffle_ratio (`float`, *optional*, defaults to `0.5`):
    The ratio used for pixel shuffling when processing images. This controls the downsampling factor
    applied to image patches. The actual downsampling ratio is calculated as `1 / (pixel_shuffle_ratio^2)`.
fake_image_token (`str`, *optional*, defaults to `"<|image|>"`):
    The placeholder token in the text that will be replaced with actual image tokens. This token serves
    as a marker indicating where images should be inserted in the text sequence.
image_token (`str`, *optional*, defaults to `"<|image|>"`):
    The token to be used to represent an image in the text.
start_of_image_token (`str`, *optional*, defaults to `"<|image_start|>"`):
    The special token that marks the beginning of an image sequence in the text. This token is prepended
    to image token sequences to delimit image boundaries.
end_of_image_token (`str`, *optional*, defaults to `"<|image_end|>"`):
    The special token that marks the end of an image sequence in the text. This token is appended to
    image token sequences to delimit image boundaries.
patch_token (`str`, *optional*, defaults to `"<|patch|>"`):
    The token used to represent individual image patches. Multiple patch tokens are used to represent
    the full image, with the number depending on the image size and patch configuration.
tile_x_separator_token (`str`, *optional*, defaults to `"<|tile_x_separator|>"`):
    The token used to separate tiles (patches) horizontally within an image. This token is inserted
    between patches in the same row when images are split into multiple tiles.
tile_y_separator_token (`str`, *optional*, defaults to `"<|tile_y_separator|>"`):
    The token used to separate tiles (patches) vertically within an image. This token is inserted
    between rows of patches when images are split into multiple tiles.
)Úchat_templateg      ð?N)ÚsuperÚ__init__r"   ÚroundÚdownsample_ratior    Úfake_image_tokenÚimage_tokenÚconvert_tokens_to_idsÚimage_token_idÚstart_of_img_tokenÚend_of_img_tokenÚimg_patch_tokenÚ
tile_tokenÚtile_global_token)ÚselfÚimage_processorÚ	tokenizerr    r!   r.   r/   Ústart_of_image_tokenÚend_of_image_tokenÚpatch_tokenÚtile_x_separator_tokenÚtile_y_separator_tokenr)   ÚkwargsÚ	__class__s   &&&&&&&&&&&&&,€r   r+   ÚLlama4Processor.__init__%   s|   ø€ ôV 	‰Ñ˜À=ÐÔQä #¤E¨#Ð1DÀaÕ1GÕ*HÓ$IÓ JˆÔØ$Œà 0ÔØ&ÔØ'×=Ñ=¸d×>NÑ>NÓOˆÔØ"6ÔØ 2ÔØ*ÔØ0ŒØ!7Ör   c                ó  € RpVw  rEWE,          ^8”  dV   \        V4       FF  p\        V4       F+  pVRV,          ,          pWu^,
          8  g   K"  VR,          pK-  	  VR,          pKH  	  VR,          pVRV,          ,          pVR,          pV# )z¤
Create a structured string representation of image tokens

Args:
   num_patches: Number of patches in the image

Returns:
    String with appropriate image tokens
r   r   r   r   r   r   )Úrange)r7   Úaspect_ratioÚnum_patches_per_chunkÚ
img_stringÚratio_hÚratio_wÚyyÚxxs   &&&     r   Ú_prompt_split_imageÚ#Llama4Processor._prompt_split_image^   s–   € ð 'ˆ
Ø'ÑˆØÕ˜qÔ Ü˜G–n�Ü ž.�BØ +Ð0EÕ"EÕE�JØ a�KÖ'Ø"Ð&<Õ<š
ñ )ð
 Ð4Õ4’
ñ %ð 	�kÕ!ˆ
Ø�kÐ$9Õ9Õ9ˆ
Ø�oÕ%ˆ
àÐr   c                óX   <€ \         SV `  ! RR VRV/VB vrpVe   \        V4      pWRR3# )ÚimagesÚtextNr   )r*   Úprepare_inputs_layoutr   )r7   rN   rO   r?   Ú_r@   s   &&&, €r   rP   Ú%Llama4Processor.prepare_inputs_layouty   sB   ø€ Ü ™7Ò8Ñ\ÀÐ\ÈTÐ\ÐU[Ñ\Ðˆ�qØÒÜ-¨fÓ5ˆFØ˜T 4Ð'Ð'r   c                óè   <a € \         SS `  ! RR VRV/VB  Vf   \        R4      hVeI   VeC   \        V 3R lV 4       4      pV\	        V4      8w  d   \        RV R\	        V4       R24      hR# R# R# )	rN   rO   NzYou have to specify text.c              3   óX   <"  € T F  qP                  SP                  4      x € K!  	  R # 5i)N)Úcountr.   )Ú.0Úpromptr7   s   & €r   Ú	<genexpr>Ú2Llama4Processor.validate_inputs.<locals>.<genexpr>…   s#   øé € Ð$\ÑW[ÈV§\¡\°$×2GÑ2G×%HÐ%HÓW[ùs   ƒ'*zFound z) placeholders across the batch, but have z flattened images.r   )r*   Úvalidate_inputsÚ
ValueErrorÚsumÚlen)r7   rN   rO   r?   Útotal_placeholdersr@   s   f&&, €r   rZ   ÚLlama4Processor.validate_inputs   s‘   ù€ Ü‰ÒÑC vÐC°DÐC¸FÒCØŠ<ÜÐ8Ó9Ð9àÒ $Ò"2Ü!$Ô$\ÑW[Ó$\Ó!\ÐØ!¤S¨£[Ô0Ü ØÐ/Ð0ð 1 Ü # F£˜}Ð,>ð@óð ñ 1ñ #3Ñr   c                ó,   <€ V ^8„  d   QhRS[ RS[RS[/# )r   Úimage_inputsÚ	image_idxÚreturn)Údictr"   Ústr)r$   r%   s   "€r   r&   r'   Œ   s(   ø€ ÷ iñ i±ð iÁð iÑSVñ ir   c                ó  € VR ,          ^ ,          P                   RR w  rE\        W@P                  ,          WPP                  ,          ,          V P                  ,          4      pV P	                  VR,          V,          V4      # )Úpixel_valuesNÚaspect_ratioséþÿÿÿ)Úshaper"   r    r-   rK   )r7   ra   rb   r?   Úimage_heightÚimage_widthrE   s   &&&,   r   Úreplace_image_tokenÚ#Llama4Processor.replace_image_tokenŒ   sn   € Ø$0°Õ$@ÀÕ$C×$IÑ$IÈ"È#Ð$NÑ!ˆÜ #ØŸ_™_Õ,°ÇÁÕ1OÕPÐTX×TiÑTiÕió!
Ðð ×'Ñ'¨°_Õ(EÀiÕ(PÐRgÓhÐhr   c                ó0   <€ V ^8„  d   QhRS[ S[,          /# )r   rc   )Úlistre   )r$   r%   s   "€r   r&   r'   ”   s   ø€ ÷ !ñ !¡D©¥Iñ !r   c                ó   € R .# )rh   r   )r7   s   &r   Úunused_input_namesÚ"Llama4Processor.unused_input_names“   s   € àÐ Ð r   )
r-   r3   r.   r/   r1   r4   r    r2   r6   r5   )NN)r   r   r   r   r   Úvalid_processor_kwargsr)   r+   rK   rP   rZ   rm   Úpropertyrr   r   Ú__classdictcell__Ú__classcell__)r@   r%   s   @@r   r   r   !   sp   ù‡ € à2Ðð ØØØ%(Ø$ØØ.Ø*ØØ5Ø5Ø#÷78õ 78òr÷6(÷÷ið ið ÷!ó ÷!ð !r   r   N)Útransformers.processing_utilsr   r   Úimage_utilsr   Úutilsr   r   r)   r   Ú__all__r   r   r   Ú<module>r|      sR   ð÷  Kå 3Ý #ôÐ,°Eõ ð ]P€ð ôs!�nó s!ó ðs!ðl Ð
‚r   