Ë
    T^(h
¡ ã            
       óÄ  — d Z ddlZddlmZmZmZmZ ddlZddlm	c m
Z ddlZddlm	Z	 ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ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' ddl(m)Z)m*Z*m+Z+  e#«       rddl,m-Z- ddl.m/Z/  e$j`                  e1«      Z2dejf                  de4de5deejf                  ejf                  f   fd„Z6dejf                  de4de4dejn                  dejf                  f
d„Z8 G d„ de	jr                  «      Z: G d„ de	jr                  «      Z; G d „ d!e	jr                  «      Z< G d"„ d#e	jr                  «      Z= G d$„ d%e=«      Z>e=e>d&œZ? G d'„ d(e	jr                  «      Z@ G d)„ d*e	jr                  «      ZA G d+„ d,e	jr                  «      ZB G d-„ d.e	jr                  «      ZC G d/„ d0eC«      ZDd1„ ZEdUd2„ZFd3ejf                  d4e4dejf                  fd5„ZG G d6„ d7e	jr                  «      ZH G d8„ d9eH«      ZIeCeDd&œZJeHeId&œZK G d:„ d;e	jr                  «      ZL G d<„ d=e	jr                  «      ZM G d>„ d?ej                  jr                  «      ZN G d@„ dAe	jr                  «      ZO G dB„ dCe«      ZPdDZQdEZRdFZSdGZT e!dHeQ«       G dI„ dJeP«      «       ZU e!dKeQ«       G dL„ dMeP«      «       ZV e!dNeQ«       G dO„ dPePe«      «       ZW e!dQeQ«       G dR„ dSePe«      «       ZXg dT¢ZYy)VzPyTorch Mllama model.é    N)ÚListÚOptionalÚTupleÚUnion)Únné   )ÚPreTrainedModel)ÚACT2FN)ÚCacheÚDynamicCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverter)ÚBaseModelOutputÚBaseModelOutputWithPastÚCausalLMOutputWithPast)ÚROPE_INIT_FUNCTIONSÚdynamic_rope_update)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_torch_flex_attn_availableÚloggingÚreplace_return_docstrings)Údeprecate_kwargé   )ÚMllamaConfigÚMllamaTextConfigÚMllamaVisionConfig)Ú	BlockMask)Úmake_flex_block_causal_maskÚcross_attention_maskÚnum_vision_tokensÚdtypeÚreturnc                 óì  — | j                   ^}}}| j                  |d¬«      } | j                  ||d«      } | j                  d«      } d| z
  j	                  |«      }|j                  |j	                  t        j                  «      t        j                  |«      j                  «      } t        j                  |«      j                  }| |k7  j                  d¬«      j                  | «      d   }| |z  } | |fS )Nr   ©Údiméÿÿÿÿr   g      ð?).N)ÚshapeÚrepeat_interleaveÚviewÚ	unsqueezeÚtoÚmasked_fillÚtorchÚboolÚfinfoÚminÚanyÚtype_as)	r!   r"   r#   Ú
batch_sizeÚtext_total_lengthÚ_Úinverted_cross_attn_maskÚnegative_inf_valueÚfull_text_row_masked_out_masks	            úh/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/mllama/modeling_mllama.pyÚ_prepare_cross_attention_maskr<   4   s  € ð )=×(BÑ(BÐ%€JÐ! AØ/×AÑAÐBSÐYZÐAÓ[ÐØ/×4Ñ4°ZÐARÐTVÓWÐØ/×9Ñ9¸!Ó<Ðð !$Ð&:Ñ :×>Ñ>¸uÓEÐØ3×?Ñ?Ø ×#Ñ#¤E§J¡JÓ/´·±¸UÓ1C×1GÑ1GóÐô Ÿ™ UÓ+×/Ñ/Ðà	Ð!3Ñ	3×8Ñ8¸RÐ8Ó@×HÑHÐI]Ó^Ð_hÑið "ð Ð9Ñ9ÐàÐ!>Ð>Ð>ó    Úaspect_ratio_maskÚnum_patchesÚtarget_lengthc                 ó|  — | j                   \  }}| j                  ||dd«      j                  |«      }|j                  dd|d«      }||z
  }d|d d …d d …| d …f<   d|z
  }|j	                  |||z  d«      }||j                  dd«      z  t        j                  |«      j                  z  }|j                  d«      }|S )Nr   r   r(   éþÿÿÿ)
r)   r+   r-   ÚrepeatÚreshapeÚ	transposer/   r1   r2   r,   )r>   r?   r@   r#   r5   Úmax_num_tilesÚattention_maskÚpad_patchess           r;   Ú$_prepare_aspect_ratio_attention_maskrI   P   s×   € ð !2× 7Ñ 7Ñ€J�Ø&×+Ñ+¨J¸ÀqÈ!ÓL×OÑOÐPUÓV€NØ#×*Ñ*¨1¨a°ÀÓB€Nð   +Ñ-€KØ*+€N’1’a˜+˜™Ð&Ñ'ð ˜Ñ'€Nð $×+Ñ+¨J¸ÈÑ8UÐWXÓY€NØ# n×&>Ñ&>¸rÀ2Ó&FÑFÌÏÉÐUZÓI[×I_ÑI_Ñ_€NØ#×-Ñ-¨aÓ0€NàÐr=   c                   óz   ‡ — e Zd Zddedefˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZ	S )	Ú%MllamaPrecomputedAspectRatioEmbeddingÚconfigÚis_gatedc                 ót  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  | _        || _        t        j                  | j                  dz   | j                  | j                  z  «      | _        |r.t        j                  t        j                  d«      «      | _        y y ©Nr   )ÚsuperÚ__init__rF   Úhidden_sizeÚmax_aspect_ratio_idrM   r   Ú	EmbeddingÚ	embeddingÚ	Parameterr/   ÚzerosÚgate©ÚselfrL   rM   Ú	__class__s      €r;   rQ   z.MllamaPrecomputedAspectRatioEmbedding.__init__l   sŽ   ø€ Ü‰ÑÔØ#×1Ñ1ˆÔØ!×-Ñ-ˆÔØ#)×#=Ñ#=ˆÔ Ø ˆŒäŸ™ d×&>Ñ&>ÀÑ&BÀD×DVÑDVÐY]×YiÑYiÑDiÓjˆŒÙÜŸ™¤U§[¡[°£^Ó4ˆD�Ið r=   Úhidden_stateÚaspect_ratio_idsr$   c                 óÔ   — | j                  |«      }|j                  d| j                  d| j                  «      }| j                  r|| j
                  j                  «       z  }||z   }|S )Nr(   r   )rU   rD   rF   rR   rM   rX   Útanh)rZ   r\   r]   Ú
embeddingss       r;   Úforwardz-MllamaPrecomputedAspectRatioEmbedding.forwardw   s_   € Ø—^‘^Ð$4Ó5ˆ
Ø×'Ñ'¨¨D×,>Ñ,>ÀÀ4×CSÑCSÓTˆ
à�=Š=Ø# d§i¡i§n¡nÓ&6Ñ6ˆJà# jÑ0ˆØÐr=   )T)
Ú__name__Ú
__module__Ú__qualname__r   r0   rQ   r/   ÚTensorra   Ú__classcell__©r[   s   @r;   rK   rK   k   s@   ø„ ñ	5Ð1ð 	5¸Tõ 	5ð E§L¡Lð ÀEÇLÁLð ÐUZ×UaÑUa÷ r=   rK   c                   ót   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZS )Ú"MllamaPrecomputedPositionEmbeddingrL   c                 óŒ  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  |j
                  z  dz  dz   | _        |j                  | _        |j                  dz  | _        t        j                  t        j                  d«      «      | _        t        j                  | j                  | j                  «      }t        j                  | j                  |z  «      | _        t        j                   | j                  dz   | j                  | j                  z  | j                  z  «      | _        y )Né   r   ç      à¿)rP   rQ   rF   rS   Ú
image_sizeÚ
patch_sizer?   rR   Úscaler   rV   r/   rW   rX   ÚrandnrU   rT   Útile_embedding)rZ   rL   Úposition_embeddingr[   s      €r;   rQ   z+MllamaPrecomputedPositionEmbedding.__init__ƒ   sþ   ø€ Ü‰ÑÔØ#×1Ñ1ˆÔØ#)×#=Ñ#=ˆÔ Ø"×-Ñ-°×1BÑ1BÑBÀqÑHÈ1ÑLˆÔØ!×-Ñ-ˆÔØ×'Ñ'¨Ñ-ˆŒ
ä—L‘L¤§¡¨Q£Ó0ˆŒ	ô #Ÿ[™[¨×)9Ñ)9¸4×;KÑ;KÓLÐÜŸ™ d§j¡jÐ3EÑ&EÓFˆŒô !Ÿl™lØ×$Ñ$ qÑ(¨$×*<Ñ*<¸t×?OÑ?OÑ*OÐRV×RbÑRbÑ*bó
ˆÕr=   r\   r]   r$   c                 ó˜  — d| j                   j                  «       z
  | j                  z  }||j                  dd| j                  | j
                  «      z   }| j                  |«      }|j                  d   }|j                  || j                  | j                  | j
                  «      }| j                   j                  «       |z  }||z   }|S )Nr   r   )
rX   r_   rU   r+   r?   rR   rq   r)   rD   rF   )rZ   r\   r]   Úgated_position_embeddingÚtile_position_embeddingr5   Úgated_tile_position_embeddings          r;   ra   z*MllamaPrecomputedPositionEmbedding.forward–   sÄ   € à$%¨¯	©	¯©Ó(8Ñ$8¸D¿N¹NÑ#JÐ Ø#Ð&>×&CÑ&CÀAÀqÈ$×JZÑJZÐ\`×\lÑ\lÓ&mÑmˆð #'×"5Ñ"5Ð6FÓ"GÐØ!×'Ñ'¨Ñ*ˆ
Ø"9×"AÑ"AØ˜×*Ñ*¨D×,<Ñ,<¸d×>NÑ>Nó#
Ðð )-¯	©	¯©Ó(8Ð;RÑ(RÐ%Ø#Ð&CÑCˆàÐr=   )	rb   rc   rd   r   rQ   r/   re   ra   rf   rg   s   @r;   ri   ri   ‚   s9   ø„ ð
Ð1õ 
ð& E§L¡Lð ÀEÇLÁLð ÐUZ×UaÑUa÷ r=   ri   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚMllamaVisionMLPc                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y ©N)rP   rQ   rL   r
   Ú
hidden_actÚactivation_fnr   ÚLinearrR   Úintermediate_sizeÚfc1Úfc2©rZ   rL   r[   s     €r;   rQ   zMllamaVisionMLP.__init__©   sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r=   Úhidden_statesr$   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rz   )r   r|   r€   )rZ   r‚   s     r;   ra   zMllamaVisionMLP.forward°   s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr=   )rb   rc   rd   rQ   r/   re   ra   rf   rg   s   @r;   rx   rx   ¨   s$   ø„ ôKð U§\¡\ð °e·l±l÷ r=   rx   c                   ó®   ‡ — e Zd Zdefˆ fd„Z	 	 ddej                  deej                     dee   de	ej                  eej                     f   fd„Z
ˆ xZS )	ÚMllamaVisionAttentionrL   c                 ó’  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  |j                  z  | _        t        j                  | j                  | j
                  | j                  z  d¬«      | _	        t        j                  | j                  | j
                  | j                  z  d¬«      | _
        t        j                  | j                  | j
                  | j                  z  d¬«      | _        t        j                  | j
                  | j                  z  | j                  d¬«      | _        y ©NF©Úbias)rP   rQ   rR   Ú	embed_dimÚattention_headsÚ	num_headsÚhead_dimr   r}   Úq_projÚk_projÚv_projÚo_projr�   s     €r;   rQ   zMllamaVisionAttention.__init__¸   sØ   ø€ Ü‰ÑÔà×+Ñ+ˆŒØ×/Ñ/ˆŒØ×*Ñ*¨f×.DÑ.DÑDˆŒä—i‘i §¡°·±ÀÇÁÑ0NÐUZÔ[ˆŒÜ—i‘i §¡°·±ÀÇÁÑ0NÐUZÔ[ˆŒÜ—i‘i §¡°·±ÀÇÁÑ0NÐUZÔ[ˆŒÜ—i‘i §¡°·±Ñ >ÀÇÁÐUZÔ[ˆ�r=   r\   rG   Úoutput_attentionsr$   c                 ó$  — | j                  |«      }| j                  |«      }| j                  |«      }|j                  \  }}}	|j                  \  }	}
}	|j	                  ||| j
                  | j                  «      j                  dd«      }|j	                  ||
| j
                  | j                  «      j                  dd«      }|j	                  ||
| j
                  | j                  «      j                  dd«      }t        j                  ||j                  dd«      «      t        j                  | j                  «      z  }|�#|d d …d d …d d …d |j                  d   …f   }||z   }t        j                  j                  |dt        j                  ¬«      j!                  |j"                  «      }t        j                  ||«      }|j                  dd«      j%                  «       }|j'                  ||d«      }| j)                  |«      }|sd }||fS )Nr   rk   r   rB   r(   ©r'   r#   )rŽ   r�   r�   r)   r+   rŒ   r�   rE   r/   ÚmatmulÚmathÚsqrtr   Ú
functionalÚsoftmaxÚfloat32r-   r#   Ú
contiguousrD   r‘   )rZ   r\   rG   r’   ÚqueryÚkeyÚvaluer5   Ú	q_seq_lenr7   Ú
kv_seq_lenÚattn_weightsÚcausal_maskÚattn_outputÚoutputs                  r;   ra   zMllamaVisionAttention.forwardÄ   s¾  € ð —‘˜LÓ)ˆØ�k‰k˜,Ó'ˆØ—‘˜LÓ)ˆà#(§;¡;Ñ ˆ
�I˜qØŸ9™9Ñˆˆ:�qà—
‘
˜: y°$·.±.À$Ç-Á-ÓP×ZÑZÐ[\Ð^_Ó`ˆØ�h‰h�z :¨t¯~©~¸t¿}¹}ÓM×WÑWÐXYÐ[\Ó]ˆØ—
‘
˜: z°4·>±>À4Ç=Á=ÓQ×[Ñ[Ð\]Ð_`Óaˆä—|‘| E¨3¯=©=¸¸AÓ+>Ó?Ä$Ç)Á)ÈDÏMÉMÓBZÑZˆàÐ%Ø(ªªAªq°/°C·I±I¸b±M°/Ð)AÑBˆKØ'¨+Ñ5ˆLô —}‘}×,Ñ,¨\¸rÌÏÉÐ,ÓW×ZÑZÐ[`×[fÑ[fÓgˆÜ—l‘l <°Ó7ˆà!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×)Ñ)¨*°iÀÓDˆà—‘˜[Ó)ˆá ØˆLà�|Ð#Ð#r=   ©NN)rb   rc   rd   r   rQ   r/   re   r   r0   r   ra   rf   rg   s   @r;   r…   r…   ·   sm   ø„ ð
\Ð1õ 
\ð 26Ø,0ñ	#$à—l‘lð#$ð ! §¡Ñ.ð#$ð $ D™>ð	#$ð
 
ˆu�|‰|˜X e§l¡lÑ3Ð3Ñ	4÷#$r=   r…   c            	       ó~   ‡ — e Zd Z	 	 ddej                  deej                     dee   dej                  fˆ fd„Zˆ xZS )ÚMllamaVisionSdpaAttentionr\   rG   r’   r$   c                 ó  •— |r't         j                  d«       t        ‰| �  |||¬«      S | j	                  |«      }| j                  |«      }| j                  |«      }|j                  \  }}}	|j                  \  }	}
}	|j                  ||| j                  | j                  «      }|j                  ||
| j                  | j                  «      }|j                  ||
| j                  | j                  «      }|j                  dd«      }|j                  dd«      }|j                  dd«      }t        j                  ||||¬«      }|j                  dd«      j                  «       }|j                  ||d«      }| j!                  |«      }|d fS )Na�  MllamaModel is using MllamaVisionSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.©r\   rG   r’   r   rk   )Ú	attn_maskr(   )ÚloggerÚwarning_oncerP   ra   rŽ   r�   r�   r)   r+   rŒ   r�   rE   ÚFÚscaled_dot_product_attentionr›   rD   r‘   )rZ   r\   rG   r’   rœ   r�   rž   r5   rŸ   r7   r    r£   r¤   r[   s                €r;   ra   z!MllamaVisionSdpaAttention.forwardì   sk  ø€ ñ Ü×Ñð[ôô ‘7‘?Ø)Ø-Ø"3ð #ó ð ð —‘˜LÓ)ˆØ�k‰k˜,Ó'ˆØ—‘˜LÓ)ˆà#(§;¡;Ñ ˆ
�I˜qØŸ9™9Ñˆˆ:�qà—
‘
˜: y°$·.±.À$Ç-Á-ÓPˆØ�h‰h�z :¨t¯~©~¸t¿}¹}ÓMˆØ—
‘
˜: z°4·>±>À4Ç=Á=ÓQˆà—‘  1Ó%ˆØ�m‰m˜A˜qÓ!ˆØ—‘  1Ó%ˆä×4Ñ4°U¸CÀÐR`Ôaˆà!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×)Ñ)¨*°iÀÓDˆà—‘˜[Ó)ˆà�tˆ|Ðr=   r¥   )	rb   rc   rd   r/   re   r   r0   ra   rf   rg   s   @r;   r§   r§   ê   sP   ø„ ð
 26Ø,0ñ	(à—l‘lð(ð ! §¡Ñ.ð(ð $ D™>ð	(ð
 
�‰÷(ñ (r=   r§   )ÚeagerÚsdpac                   óx   ‡ — e Zd Zddedefˆ fd„Z	 	 d	dej                  deej                     dee   fd„Z	ˆ xZ
S )
ÚMllamaVisionEncoderLayerrL   rM   c                 óº  •— t         ‰| �  «        |j                  | _        |j                  | _        || _        |j                  | _        t        |j                     |«      | _	        t        |«      | _        t        j                  | j                  |j                  ¬«      | _        t        j                  | j                  |j                  ¬«      | _        |rƒt        j"                  t%        j&                  d«      t(        j*                  z  dz  «      | _        t        j"                  t%        j&                  d«      t(        j*                  z  dz  «      | _        y y )N©Úepsr   é   )rP   rQ   rR   r‹   Únum_attention_headsrM   r~   ÚMLLAMA_VISION_ATTENTION_CLASSESÚ_attn_implementationÚ	self_attnrx   Úmlpr   Ú	LayerNormÚnorm_epsÚinput_layernormÚpost_attention_layernormrV   r/   Úonesr–   ÚpiÚ	gate_attnÚgate_ffnrY   s      €r;   rQ   z!MllamaVisionEncoderLayer.__init__  sñ   ø€ Ü‰ÑÔà!×-Ñ-ˆÔØ#)×#9Ñ#9ˆÔ Ø ˆŒØ!'×!9Ñ!9ˆÔä8¸×9TÑ9TÑUÐV\Ó]ˆŒÜ" 6Ó*ˆŒä!Ÿ|™|¨D×,<Ñ,<À&Ç/Á/ÔRˆÔÜ(*¯©°T×5EÑ5EÈ6Ï?É?Ô([ˆÔ%áÜŸ\™\¬%¯*©*°Q«-¼$¿'¹'Ñ*AÀAÑ*EÓFˆDŒNÜŸL™L¬¯©°A«¼¿¹Ñ)@À1Ñ)DÓEˆD�Mð r=   r\   rG   r’   c                 ón  — |}| j                  |«      }| j                  ||¬«      \  }}| j                  r| j                  j	                  «       |z  }||z   }|}| j                  |«      }| j                  |«      }| j                  r| j                  j	                  «       |z  }||z   }|f}|r||fz  }|S )N)rG   )r¾   rº   rM   rÂ   r_   r¿   r»   rÃ   )rZ   r\   rG   r’   Úresidualr¡   Úoutputss          r;   ra   z MllamaVisionEncoderLayer.forward-  sÁ   € ð  ˆØ×+Ñ+¨LÓ9ˆØ%)§^¡^°LÐQ_ ^Ó%`Ñ"ˆ�lØ�=Š=ØŸ>™>×.Ñ.Ó0°<Ñ?ˆLØ ,Ñ.ˆð  ˆØ×4Ñ4°\ÓBˆØ—x‘x Ó-ˆØ�=Š=ØŸ=™=×-Ñ-Ó/°,Ñ>ˆLØ ,Ñ.ˆà�/ˆáØ˜�Ñ&ˆGàˆr=   ©Fr¥   )rb   rc   rd   r   r0   rQ   r/   re   r   ra   rf   rg   s   @r;   r²   r²     sT   ø„ ñFÐ1ð F¸Tõ Fð* 26Ø,0ñ	à—l‘lðð ! §¡Ñ.ðð $ D™>÷	r=   r²   c                   óž   ‡ — e Zd ZdZddefˆ fd„Z	 	 	 	 ddej                  deej                     dee	   dee	   dee	   d	e
eef   fd
„Zˆ xZS )ÚMllamaVisionEncoderz±
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`MllamaEncoderLayer`].

    Args:
        config: MllamaConfig
    rL   c           	      óÌ   •— t         ‰| �  «        || _        t        j                  t        |«      D �cg c]  }t        ||«      ‘Œ c}«      | _        d| _        || _        y c c}w )NF)	rP   rQ   rL   r   Ú
ModuleListÚranger²   ÚlayersÚgradient_checkpointing)rZ   rL   Ú
num_layersrM   r7   r[   s        €r;   rQ   zMllamaVisionEncoder.__init__T  sU   ø€ Ü‰ÑÔØˆŒÜ—m‘mÔY^Ð_iÓYjÖ$kÐTUÔ%=¸fÀhÕ%OÒ$kÓlˆŒØ&+ˆÔ#Øˆ�ùò %ls   ³A!r‚   rG   r’   Úoutput_hidden_statesÚreturn_dictr$   c                 óæ  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|rdnd}|rdnd}| j                  D ]\  }|r||fz   }| j
                  r+| j                  r| j                  |j                  |||«      }	n ||||¬«      }	|r	||	d   fz   }|	d   }Œ^ |r||fz   }|st        d„ |||fD «       «      S t        |||¬«      S )ad  
        Args:
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.
                This is useful if you want more control over how to convert `input_ids` indices into associated vectors
                than the model's internal embedding lookup matrix.
            attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.

                [What are attention masks?](../glossary#attention-mask)
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
                for more detail.
            return_dict (`bool`, *optional*):
                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        N© r©   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrz   rÓ   ©Ú.0Úvs     r;   ú	<genexpr>z.MllamaVisionEncoder.forward.<locals>.<genexpr>œ  s   è ø€ Òe˜qÐWXÑWdœÑeùó   ‚Š©Úlast_hidden_stater‚   Ú
attentions)rL   r’   rÐ   Úuse_return_dictrÍ   rÎ   ÚtrainingÚ_gradient_checkpointing_funcÚ__call__Útupler   )
rZ   r‚   rG   r’   rÐ   rÑ   Úencoder_statesÚall_attentionsÚencoder_layerÚlayer_outputss
             r;   ra   zMllamaVisionEncoder.forward[  s0  € ð< 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆá3™¸ˆÙ0™°dˆà!Ÿ[™[ò 	-ˆMÙ#Ø!/°=Ð2BÑ!B�Ø×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø%ó	!‘ñ !.Ø!.Ø#1Ø&7ô!�ñ !Ø!/°=ÀÑ3CÐ2EÑ!E�à)¨!Ñ,‰Mð)	-ñ,  Ø+¨}Ð.>Ñ>ˆNáÜÑe ]°NÀNÐ$SÔeÓeÐeÜØ+¸>ÐVdô
ð 	
r=   )é    F)NNNN)rb   rc   rd   Ú__doc__r   rQ   r/   re   r   r0   r   r   r   ra   rf   rg   s   @r;   rÉ   rÉ   K  sŒ   ø„ ññÐ1õ ð 26Ø,0Ø/3Ø&*ñD
à—|‘|ðD
ð ! §¡Ñ.ðD
ð $ D™>ð	D
ð
 ' t™nðD
ð ˜d‘^ðD
ð 
ˆu�oÐ%Ñ	&÷D
r=   rÉ   c                   ó,   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Zˆ xZS )ÚMllamaTextRMSNormc                 óŠ   •— t         ‰| �  «        t        j                  t	        j
                  |«      «      | _        || _        y)z@
        MllamaTextRMSNorm is equivalent to T5LayerNorm
        N)rP   rQ   r   rV   r/   rÀ   ÚweightÚvariance_epsilon)rZ   rR   rµ   r[   s      €r;   rQ   zMllamaTextRMSNorm.__init__¤  s1   ø€ ô 	‰ÑÔÜ—l‘l¤5§:¡:¨kÓ#:Ó;ˆŒØ #ˆÕr=   c                 ó"  — |j                   }|j                  t        j                  «      }|j	                  d«      j                  dd¬«      }|t        j                  || j                  z   «      z  }| j                  |j                  |«      z  S )Nrk   r(   T)Úkeepdim)	r#   r-   r/   rš   ÚpowÚmeanÚrsqrtrì   rë   )rZ   r‚   Úinput_dtypeÚvariances       r;   ra   zMllamaTextRMSNorm.forward¬  sy   € Ø#×)Ñ)ˆØ%×(Ñ(¬¯©Ó7ˆØ ×$Ñ$ QÓ'×,Ñ,¨R¸Ð,Ó>ˆØ%¬¯©°H¸t×?TÑ?TÑ4TÓ(UÑUˆØ�{‰{˜]×-Ñ-¨kÓ:Ñ:Ð:r=   c                 ó^   — t        | j                  j                  «      › d| j                  › �S )Nz, eps=)rá   rë   r)   rì   ©rZ   s    r;   Ú
extra_reprzMllamaTextRMSNorm.extra_repr³  s*   € Ü˜Ÿ™×)Ñ)Ó*Ð+¨6°$×2GÑ2GÐ1HÐIÐIr=   )g�íµ ÷Æ°>)rb   rc   rd   rQ   ra   rö   rf   rg   s   @r;   ré   ré   £  s   ø„ õ$ò;öJr=   ré   c                   ó<  ‡ — e Zd ZdZ	 	 ddee   dee   fˆ fd„Z	 	 	 	 	 	 ddej                  deej                     dee
   deej                     d	ed
ee   deej                     deej                  eej                     eeej                        f   fd„Zˆ xZS )ÚMllamaTextCrossAttentionz=Multi-headed attention from 'Attention Is All You Need' paperrL   Ú	layer_idxc                 óî  •— t         ‰| �  «        || _        | j                  j                  | _        | j                  j
                  | _        |j                  | _        |j                  | _        |j                  | j                  z  | _        || _	        | j                  | j
                  z  | _
        t        j                  | j                  | j                  | j                  z  d¬«      | _        t        j                  | j                  | j
                  | j                  z  d¬«      | _        t        j                  | j                  | j
                  | j                  z  d¬«      | _        t        j                  | j                  | j                  z  | j                  d¬«      | _        t#        | j                  |j$                  ¬«      | _        t#        | j                  |j$                  ¬«      | _        y )NFrˆ   r´   )rP   rQ   rL   r·   rŒ   Únum_key_value_headsÚdropoutrR   r�   rù   Únum_key_value_groupsr   r}   rŽ   r�   r�   r‘   ré   Úrms_norm_epsÚq_normÚk_norm©rZ   rL   rù   r[   s      €r;   rQ   z!MllamaTextCrossAttention.__init__º  s[  ø€ ô
 	‰ÑÔØˆŒØŸ™×8Ñ8ˆŒØ#'§;¡;×#BÑ#BˆÔ Ø—~‘~ˆŒØ!×-Ñ-ˆÔØ×*Ñ*¨d¯n©nÑ<ˆŒØ"ˆŒØ$(§N¡N°d×6NÑ6NÑ$NˆÔ!ä—i‘i × 0Ñ 0°$·.±.À4Ç=Á=Ñ2PÐW\Ô]ˆŒÜ—i‘i × 0Ñ 0°$×2JÑ2JÈTÏ]É]Ñ2ZÐafÔgˆŒÜ—i‘i × 0Ñ 0°$×2JÑ2JÈTÏ]É]Ñ2ZÐafÔgˆŒÜ—i‘i §¡°·±Ñ >À×@PÑ@PÐW\Ô]ˆŒä'¨¯©¸6×;NÑ;NÔOˆŒÜ'¨¯©¸6×;NÑ;NÔOˆ�r=   r‚   Úcross_attention_statesÚpast_key_valuerG   r’   Ú	use_cacheÚcache_positionr$   c                 óò  — |j                  «       \  }}	}
| j                  |«      }|j                  ||	| j                  | j                  «      j                  dd«      }| j                  |«      }|�õ| j                  |«      }| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|j                  |d| j                  | j                  «      j                  dd«      }t        || j                  «      }t        || j                  «      }| j                  |«      }|�j|j                  ||| j                  d|i«      \  }}nF|d   dk7  r3|j                  | j                     |j                   | j                     }}nt#        d«      ‚t%        j&                  ||j                  dd«      «      t)        j*                  | j                  «      z  }|�#|dd…dd…dd…d|j,                  d	   …f   }||z   }t.        j0                  j3                  |dt$        j4                  ¬
«      j7                  |j8                  «      }t.        j0                  j;                  || j:                  | j<                  ¬«      }t%        j&                  ||«      }|j                  dd«      j?                  «       }|jA                  ||	d«      }| jC                  |«      }|sd}|||fS )ú#Input shape: Batch x Time x Channelr   rk   Nr(   r  r   ú^Cross attention layer can't find neither `cross_attn_states` nor cached values for key/values!r   rB   r”   ©ÚprÞ   )"ÚsizerŽ   r+   rŒ   r�   rE   rÿ   r�   r�   rû   Ú	repeat_kvrý   r   Úupdaterù   Ú	key_cacheÚvalue_cacheÚ
ValueErrorr/   r•   r–   r—   r)   r   r˜   r™   rš   r-   r#   rü   rÞ   r›   rD   r‘   )rZ   r‚   r  r  rG   r’   r  r  ÚbszÚq_lenr7   Úquery_statesÚ
key_statesÚvalue_statesr¡   r¢   r£   s                    r;   ra   z MllamaTextCrossAttention.forwardÑ  s·  € ð &×*Ñ*Ó,‰ˆˆU�AØ—{‘{ =Ó1ˆØ#×(Ñ(¨¨e°T·^±^ÀTÇ]Á]ÓS×]Ñ]Ð^_ÐabÓcˆØ—{‘{ <Ó0ˆà!Ð-ØŸ™Ð%;Ó<ˆJØŸ;™;Ð'=Ó>ˆLØ#Ÿ™¨¨b°$×2JÑ2JÈDÏMÉMÓZ×dÑdÐefÐhiÓjˆJØ'×,Ñ,¨S°"°d×6NÑ6NÐPT×P]ÑP]Ó^×hÑhÐijÐlmÓnˆLÜ" :¨t×/HÑ/HÓIˆJÜ$ \°4×3LÑ3LÓMˆLàŸ™ ZÓ0ˆJØÐ)ð ,:×+@Ñ+@Ø ¨d¯n©nÐ?OÐQ_Ð>`ó,Ñ(�
™Lð ˜AÑ !Ò#à×(Ñ(¨¯©Ñ8Ø×*Ñ*¨4¯>©>Ñ:ð %‰Jô
 Øpóð ô —|‘| L°*×2FÑ2FÀqÈ!Ó2LÓMÔPT×PYÑPYÐZ^×ZgÑZgÓPhÑhˆàÐ%Ø(ªªAªqÐ2H°J×4DÑ4DÀRÑ4HÐ2HÐ)HÑIˆKØ'¨+Ñ5ˆLä—}‘}×,Ñ,¨\¸rÌÏÉÐ,ÓW×ZÑZÐ[g×[mÑ[mÓnˆÜ—}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,ÓbˆÜ—l‘l <°Ó>ˆØ!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×)Ñ)¨#¨u°bÓ9ˆØ—k‘k +Ó.ˆá ØˆLà˜L¨.Ð8Ð8r=   r¥   ©NNNFNN)rb   rc   rd   rç   r   r   ÚintrQ   r/   re   r   r0   Ú
LongTensorr   ra   rf   rg   s   @r;   rø   rø   ·  sé   ø„ ÙGð .2Ø#'ñPàÐ)Ñ*ðPð ˜C‘=õPð4 :>Ø*.Ø15Ø"'Ø$(Ø59ñ99à—|‘|ð99ð !)¨¯©Ñ 6ð99ð ! ™ð	99ð
 ! §¡Ñ.ð99ð  ð99ð ˜D‘>ð99ð ! ×!1Ñ!1Ñ2ð99ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷99r=   rø   c                   ó  ‡ — e Zd ZdZ	 	 	 	 	 	 ddej
                  deej
                     dee   deej
                     dedee   deej                     d	e
ej
                  eej
                     ee
ej
                        f   fˆ fd
„Zˆ xZS )ÚMllamaTextCrossSdpaAttentiona  
    Mllama attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
    `MllamaTextCrossAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
    SDPA API.
    r‚   r  r  rG   r’   r  r  r$   c           	      ó€  •— |r+t         j                  d«       t        ‰| �  |||||||¬«      S |j	                  «       \  }}	}
| j                  |«      }|j                  ||	| j                  | j                  «      j                  dd«      }| j                  |«      }|�¸| j                  |«      }| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|j                  |d| j                  | j                  «      j                  dd«      }|�j|j                  ||| j                  d|i«      \  }}nF|d   dk7  r3|j                   | j                     |j"                  | j                     }}nt%        d	«      ‚t'        || j(                  «      }t'        || j(                  «      }| j+                  |«      }|j,                  j.                  d
k(  r2|�0|j1                  «       }|j1                  «       }|j1                  «       }|€|	dkD  rdnd}t2        j4                  j6                  j9                  ||||| j:                  r| j<                  nd|¬«      }|j                  dd«      j1                  «       }|j?                  ||	d«      }| jA                  |«      }|d|fS )r  a’  MllamaModel is using MllamaTextCrossSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.)r‚   r  rG   r  r’   r  r  r   rk   Nr(   r  r   r  ÚcudaTFç        ©rª   Ú	dropout_pÚ	is_causal)!r«   r¬   rP   ra   r  rŽ   r+   rŒ   r�   rE   rÿ   r�   r�   rû   r  rù   r  r  r  r  rý   r   ÚdeviceÚtyper›   r/   r   r˜   r®   rÞ   rü   rD   r‘   )rZ   r‚   r  r  rG   r’   r  r  r  r  r7   r  r  r  r   r£   r[   s                   €r;   ra   z$MllamaTextCrossSdpaAttention.forward  s¿  ø€ ñ ä×Ñð[ôô ‘7‘?Ø+Ø'=Ø-Ø-Ø"3Ø#Ø-ð #ó ð ð &×*Ñ*Ó,‰ˆˆU�AØ—{‘{ =Ó1ˆØ#×(Ñ(¨¨e°T·^±^ÀTÇ]Á]ÓS×]Ñ]Ð^_ÐabÓcˆØ—{‘{ <Ó0ˆà!Ð-ØŸ™Ð%;Ó<ˆJØŸ;™;Ð'=Ó>ˆLØ#Ÿ™¨¨b°$×2JÑ2JÈDÏMÉMÓZ×dÑdÐefÐhiÓjˆJØ'×,Ñ,¨S°"°d×6NÑ6NÐPT×P]ÑP]Ó^×hÑhÐijÐlmÓnˆLàÐ)ð ,:×+@Ñ+@Ø ¨d¯n©nÐ?OÐQ_Ð>`ó,Ñ(�
™Lð ˜AÑ !Ò#à×(Ñ(¨¯©Ñ8Ø×*Ñ*¨4¯>©>Ñ:ð %‰Jô
 Øpóð ô ˜z¨4×+DÑ+DÓEˆ
Ü  ¨t×/HÑ/HÓIˆà—[‘[ Ó,ˆ
ð ×Ñ×#Ñ# vÒ-°.Ð2LØ'×2Ñ2Ó4ˆLØ#×.Ñ.Ó0ˆJØ'×2Ñ2Ó4ˆLð +Ð2°u¸q²y‘DÀeˆ	ä—h‘h×)Ñ)×FÑFØØØØ$Ø&*§m¢m�d—l’l¸Øð Gó 
ˆð "×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×)Ñ)¨#¨u°bÓ9ˆØ—k‘k +Ó.ˆà˜D .Ð0Ð0r=   r  )rb   rc   rd   rç   r/   re   r   r   r0   r  r   ra   rf   rg   s   @r;   r  r    sÎ   ø„ ñð :>Ø*.Ø15Ø"'Ø$(Ø59ñS1à—|‘|ðS1ð !)¨¯©Ñ 6ðS1ð ! ™ð	S1ð
 ! §¡Ñ.ðS1ð  ðS1ð ˜D‘>ðS1ð ! ×!1Ñ!1Ñ2ðS1ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷S1ñ S1r=   r  c                 óš   — | dd| j                   d   dz  …f   }| d| j                   d   dz  d…f   }t        j                  | |fd¬«      S )z*Rotates half the hidden dims of the input..Nr(   rk   r&   )r)   r/   Úcat)ÚxÚx1Úx2s      r;   Úrotate_halfr(  l  sZ   € à	
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9‰9�r�c˜2�Y BÔ'Ð'r=   c                 óž   — |j                  |«      }|j                  |«      }| |z  t        | «      |z  z   }||z  t        |«      |z  z   }||fS )aÛ  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`, *optional*):
            Deprecated and unused.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    )r,   r(  )ÚqÚkÚcosÚsinÚposition_idsÚunsqueeze_dimÚq_embedÚk_embeds           r;   Úapply_rotary_pos_embr2  t  sY   € ð( �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€CØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�GÐÐr=   r‚   Ún_repc                 óª   — | j                   \  }}}}|dk(  r| S | dd…dd…ddd…dd…f   j                  |||||«      } | j                  |||z  ||«      S )zÔ
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r   N)r)   ÚexpandrD   )r‚   r3  Úbatchrû   Úslenr�   s         r;   r  r  �  so   € ð
 2?×1DÑ1DÑ.€EÐ  hØ�‚zØÐØ!¢!¢Q¨ªa²Ð"2Ñ3×:Ñ:¸5ÐBUÐW\Ð^bÐdlÓm€MØ× Ñ  Ð(;¸eÑ(CÀTÈ8ÓTÐTr=   c                   óŠ   ‡ — e Zd Zdedefˆ fd„Z	 	 	 	 d
dej                  dej                  dej                  dedef
d	„Z	ˆ xZ
S )ÚMllamaTextSelfAttentionrL   rù   c                 óP  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  | j                  z  | _        | j                  | j                  z  | _	        |j                  | _
        || _        t        j                  | j                  | j                  | j                  z  d¬«      | _        t        j                  | j                  | j                  | j                  z  d¬«      | _        t        j                  | j                  | j                  | j                  z  d¬«      | _        t        j                  | j                  | j                  z  | j                  d¬«      | _        y r‡   )rP   rQ   rL   r·   rŒ   rü   rR   rû   r�   rý   Ú
rope_thetarù   r   r}   rŽ   r�   r�   r‘   r  s      €r;   rQ   z MllamaTextSelfAttention.__init__�  s.  ø€ Ü‰ÑÔØˆŒØ×3Ñ3ˆŒØ—~‘~ˆŒØ!×-Ñ-ˆÔØ#)×#=Ñ#=ˆÔ Ø×*Ñ*¨d¯n©nÑ<ˆŒØ$(§N¡N°d×6NÑ6NÑ$NˆÔ!Ø ×+Ñ+ˆŒØ"ˆŒä—i‘i × 0Ñ 0°$·.±.À4Ç=Á=Ñ2PÐW\Ô]ˆŒÜ—i‘i × 0Ñ 0°$×2JÑ2JÈTÏ]É]Ñ2ZÐafÔgˆŒÜ—i‘i × 0Ñ 0°$×2JÑ2JÈTÏ]É]Ñ2ZÐafÔgˆŒÜ—i‘i §¡°·±Ñ >À×@PÑ@PÐW\Ô]ˆ�r=   r‚   rG   Úposition_embeddingsr’   r  c                 óP  — |j                  «       \  }	}
}| j                  |«      }| j                  |«      }| j                  |«      }|j	                  |	|
| j
                  | j                  «      j                  dd«      }|j	                  |	|
| j                  | j                  «      j                  dd«      }|j	                  |	|
| j                  | j                  «      j                  dd«      }|\  }}t        ||||«      \  }}|�'|||dœ}|j                  ||| j                  |«      \  }}t        || j                  «      }t        || j                  «      }t        j                  ||j                  dd«      «      t!        j"                  | j                  «      z  }|�#|d d …d d …d d …d |j$                  d   …f   }||z   }t&        j(                  j+                  |dt        j,                  ¬«      j/                  |j0                  «      }t&        j(                  j3                  || j2                  | j4                  ¬«      }t        j                  ||«      }|j                  dd«      j7                  «       }|j	                  |	|
d«      }| j9                  |«      }|sd }|||fS )	Nr   rk   ©r-  r,  r  r   rB   r(   r”   r	  )r  rŽ   r�   r�   r+   rŒ   r�   rE   rû   r2  r  rù   r  rý   r/   r•   r–   r—   r)   r   r˜   r™   rš   r-   r#   rü   rÞ   r›   r‘   )rZ   r‚   rG   r<  r’   r  r  r  Úkwargsr  r  r7   r  r  r  r,  r-  Úcache_kwargsr¡   r¢   r£   s                        r;   ra   zMllamaTextSelfAttention.forward®  sh  € ð &×*Ñ*Ó,‰ˆˆU�Aà—{‘{ =Ó1ˆØ—[‘[ Ó/ˆ
Ø—{‘{ =Ó1ˆà#×(Ñ(¨¨e°T·^±^ÀTÇ]Á]ÓS×]Ñ]Ð^_ÐabÓcˆØ—_‘_ S¨%°×1IÑ1IÈ4Ï=É=ÓY×cÑcÐdeÐghÓiˆ
Ø#×(Ñ(¨¨e°T×5MÑ5MÈtÏ}É}Ó]×gÑgÐhiÐklÓmˆà&‰ˆˆSÜ#7¸ÀjÐRUÐWZÓ#[Ñ ˆ�jàÐ%à#&¨sÀnÑUˆLØ'5×'<Ñ'<¸ZÈÐW[×WeÑWeÐgsÓ'tÑ$ˆJ˜ä˜z¨4×+DÑ+DÓEˆ
Ü  ¨t×/HÑ/HÓIˆä—|‘| L°*×2FÑ2FÀqÈ!Ó2LÓMÔPT×PYÑPYÐZ^×ZgÑZgÓPhÑhˆàÐ%Ø(ªªAªqÐ2H°J×4DÑ4DÀRÑ4HÐ2HÐ)HÑIˆKØ'¨+Ñ5ˆLô —}‘}×,Ñ,¨\¸rÌÏÉÐ,ÓW×ZÑZÐ[g×[mÑ[mÓnˆÜ—}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,ÓbˆÜ—l‘l <°Ó>ˆà!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×&Ñ& s¨E°2Ó6ˆà—k‘k +Ó.ˆá ØˆLà˜L¨.Ð8Ð8r=   ©FFNN)rb   rc   rd   r   r  rQ   r/   re   r0   ra   rf   rg   s   @r;   r9  r9  œ  si   ø„ ð^Ð/ð ^¸Cõ ^ð, #(ØØØñ39à—|‘|ð39ð Ÿ™ð39ð #Ÿ\™\ð	39ð
  ð39ð ÷39r=   r9  c                   óz   ‡ — e Zd Z	 	 	 	 ddej                  dej                  dej                  dedef
ˆ fd„Zˆ xZS )ÚMllamaTextSelfSdpaAttentionr‚   rG   r<  r’   r  c                 ó$  •— |r,t         j                  d«       t        ‰| �  d|||||||dœ|¤ŽS |j	                  «       \  }	}
}| j                  |«      }| j                  |«      }| j                  |«      }|j                  |	|
| j                  | j                  «      j                  dd«      }|j                  |	|
| j                  | j                  «      j                  dd«      }|j                  |	|
| j                  | j                  «      j                  dd«      }|\  }}t        ||||«      \  }}|�'|||dœ}|j                  ||| j                  |«      \  }}t!        || j"                  «      }t!        || j"                  «      }|}|�|d d …d d …d d …d |j$                  d   …f   }|j&                  j(                  dk(  r2|�0|j+                  «       }|j+                  «       }|j+                  «       }|€|
dkD  rdnd	}t,        j.                  j0                  j3                  ||||| j4                  r| j6                  nd
|¬«      }|j                  dd«      j+                  «       }|j                  |	|
d«      }| j9                  |«      }|d |fS )Na‘  MllamaModel is using MllamaTextSelfSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.)r‚   rG   r<  r  r’   r  r  r   rk   r>  rB   r  TFr  r  r(   rÓ   )r«   r¬   rP   ra   r  rŽ   r�   r�   r+   rŒ   r�   rE   rû   r2  r  rù   r  rý   r)   r!  r"  r›   r/   r   r˜   r®   rÞ   rü   r‘   )rZ   r‚   rG   r<  r’   r  r  r  r?  r  r  r7   r  r  r  r,  r-  r@  r¢   r   r£   r[   s                        €r;   ra   z#MllamaTextSelfSdpaAttention.forwardæ  s§  ø€ ñ ä×Ñð[ôô ‘7‘?ð 	Ø+Ø-Ø$7Ø-Ø"3Ø#Ø-ñ	ð ñ	ð 	ð &×*Ñ*Ó,‰ˆˆU�Aà—{‘{ =Ó1ˆØ—[‘[ Ó/ˆ
Ø—{‘{ =Ó1ˆà#×(Ñ(¨¨e°T·^±^ÀTÇ]Á]ÓS×]Ñ]Ð^_ÐabÓcˆØ—_‘_ S¨%°×1IÑ1IÈ4Ï=É=ÓY×cÑcÐdeÐghÓiˆ
Ø#×(Ñ(¨¨e°T×5MÑ5MÈtÏ}É}Ó]×gÑgÐhiÐklÓmˆà&‰ˆˆSÜ#7¸ÀjÐRUÐWZÓ#[Ñ ˆ�jàÐ%à#&¨sÀnÑUˆLØ'5×'<Ñ'<¸ZÈÐW[×WeÑWeÐgsÓ'tÑ$ˆJ˜ä˜z¨4×+DÑ+DÓEˆ
Ü  ¨t×/HÑ/HÓIˆà$ˆØÐ%Ø%¢aªªAÐ/E°×1AÑ1AÀ"Ñ1EÐ/EÐ&EÑFˆKð ×Ñ×#Ñ# vÒ-°+Ð2IØ'×2Ñ2Ó4ˆLØ#×.Ñ.Ó0ˆJØ'×2Ñ2Ó4ˆLð (Ð/°E¸A²I‘DÀ5ˆ	ä—h‘h×)Ñ)×FÑFØØØØ!Ø&*§m¢m�d—l’l¸Øð Gó 
ˆð "×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×&Ñ& s¨E°2Ó6ˆà—k‘k +Ó.ˆØ˜D .Ð0Ð0r=   rA  )rb   rc   rd   r/   re   r0   ra   rf   rg   s   @r;   rC  rC  ä  s^   ø„ ð #(ØØØñM1à—|‘|ðM1ð Ÿ™ðM1ð #Ÿ\™\ð	M1ð
  ðM1ð ÷M1ñ M1r=   rC  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚMllamaTextMLPc                 óÊ  •— t         ‰| �  «        || _        |j                  | _        |j                  | _        t        j                  | j                  | j                  d¬«      | _        t        j                  | j                  | j                  d¬«      | _        t        j                  | j                  | j                  d¬«      | _	        t        |j                     | _        y r‡   )rP   rQ   rL   rR   r~   r   r}   Ú	gate_projÚup_projÚ	down_projr
   r{   Úact_fnr�   s     €r;   rQ   zMllamaTextMLP.__init__<  s¦   ø€ Ü‰ÑÔØˆŒØ!×-Ñ-ˆÔØ!'×!9Ñ!9ˆÔÜŸ™ 4×#3Ñ#3°T×5KÑ5KÐRWÔXˆŒÜ—y‘y ×!1Ñ!1°4×3IÑ3IÐPUÔVˆŒÜŸ™ 4×#9Ñ#9¸4×;KÑ;KÐRWÔXˆŒä˜V×.Ñ.Ñ/ˆ�r=   c                 óˆ   — | j                  | j                  | j                  |«      «      | j                  |«      z  «      }|S rz   )rJ  rK  rH  rI  )rZ   r%  rJ  s      r;   ra   zMllamaTextMLP.forwardG  s6   € Ø—N‘N 4§;¡;¨t¯~©~¸aÓ/@Ó#AÀDÇLÁLÐQRÃOÑ#SÓTˆ	ØÐr=   )rb   rc   rd   rQ   ra   rf   rg   s   @r;   rF  rF  ;  s   ø„ ô	0ör=   rF  c                   óä  ‡ — e Zd Zdedefˆ fd„Z	 	 	 	 	 	 	 	 	 	 ddej                  deej                     deej                     deej                     dee	ej                  ej                  f      d	eej                     d
ee   dee   dee   deej                     dee	ej                  ej                  f      de	ej                  ee	ej                  ej                  f      f   fd„Zˆ xZS )ÚMllamaSelfAttentionDecoderLayerrL   rù   c                 óH  •— t         ‰| �  «        |j                  | _        t        |j                     ||¬«      | _        t        |«      | _        t        |j                  |j                  ¬«      | _
        t        |j                  |j                  ¬«      | _        || _        y )N)rL   rù   r´   )rP   rQ   rR   ÚMLLAMA_TEXT_ATTENTION_CLASSESr¹   rº   rF  r»   ré   rþ   r¾   r¿   rù   r  s      €r;   rQ   z(MllamaSelfAttentionDecoderLayer.__init__N  s�   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔä6°v×7RÑ7RÑSÐ[aÐmvÔwˆŒä  Ó(ˆŒÜ0°×1CÑ1CÈ×I\ÑI\Ô]ˆÔÜ(9¸&×:LÑ:LÐRX×ReÑReÔ(fˆÔ%à"ˆ�r=   r‚   r  r!   rG   r:   r.  r  r’   r  r  r<  r$   c           
      óè   — |}| j                  |«      }| j                  ||||||	|
|¬«      \  }}}||z   }|}| j                  |«      }| j                  |«      }||z   }|f}|r||fz  }|	r||fz  }|S )a.  
        Args:
            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            attention_mask (`torch.FloatTensor`, *optional*):
                attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1,
                query_sequence_length, key_sequence_length)` if default attention is used.
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            use_cache (`bool`, *optional*):
                If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
                (see `past_key_values`).
            past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
            cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
                Indices depicting the position of the input sequence tokens in the sequence
            position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*):
                Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
                with `head_dim` being the embedding dimension of each attention head.
            kwargs (`dict`, *optional*):
                Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
                into the model
        )r‚   rG   r.  r  r’   r  r  r<  )r¾   rº   r¿   r»   )rZ   r‚   r  r!   rG   r:   r.  r  r’   r  r  r<  rÅ   Úself_attn_weightsÚpresent_key_valuerÆ   s                   r;   ra   z'MllamaSelfAttentionDecoderLayer.forwardZ  s¼   € ðH !ˆà×,Ñ,¨]Ó;ˆð ?C¿n¹nØ'Ø)Ø%Ø)Ø/ØØ)Ø 3ð ?Mó 	?
Ñ;ˆÐ(Ð*;ð ! =Ñ0ˆð !ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆáØÐ)Ð+Ñ+ˆGáØÐ)Ð+Ñ+ˆGàˆr=   )
NNNNNNFFNN)rb   rc   rd   r   r  rQ   r/   re   r   r   r  r   r0   ÚFloatTensorra   rf   rg   s   @r;   rN  rN  M  sY  ø„ ð
#Ð/ð 
#¸Cõ 
#ð :>Ø7;Ø15ØUYØ37Ø*.Ø,1Ø$)Ø59ØKOñCà—|‘|ðCð !)¨¯©Ñ 6ðCð ' u§|¡|Ñ4ð	Cð
 ! §¡Ñ.ðCð (0°°e·l±lÀEÇLÁLÐ6PÑ0QÑ'RðCð ˜u×/Ñ/Ñ0ðCð ! ™ðCð $ D™>ðCð ˜D‘>ðCð ! ×!1Ñ!1Ñ2ðCð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHðCð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷Cr=   rN  c                   ór  ‡ — e Zd ZdZdededdfˆ fd„Z	 	 	 	 	 	 ddej                  dej                  d	ej                  d
ej                  de	ej                  ej                  f   de
ej                     de
e   de
e   de
e   de
ej                     de
ej                     de	ej                     fd„Zˆ xZS )Ú MllamaCrossAttentionDecoderLayerzLCross-attention transformer block with tanh-gated attention and feedforward.rL   rù   r$   Nc                 ó  •— t         ‰| �  «        || _        t        |j                     ||¬«      | _        t        |j                  |j                  ¬«      | _	        t        j                  j                  t        j                  d«      «      | _        t        |«      | _        t        |j                  |j                  ¬«      | _        t        j                  j                  t        j                  d«      «      | _        y )N)rù   r´   r   )rP   rQ   rù   Ú#MLLAMA_TEXT_CROSS_ATTENTION_CLASSESr¹   Ú
cross_attnré   rR   rþ   r¾   r/   r   rV   rW   Úcross_attn_attn_gaterF  r»   r¿   Úcross_attn_mlp_gater  s      €r;   rQ   z)MllamaCrossAttentionDecoderLayer.__init__£  s±   ø€ Ü‰ÑÔØ"ˆŒÜ=¸f×>YÑ>YÑZÐ[aÐmvÔwˆŒä0°×1CÑ1CÈ×I\ÑI\Ô]ˆÔÜ$)§H¡H×$6Ñ$6´u·{±{À1³~Ó$FˆÔ!ä  Ó(ˆŒÜ(9¸&×:LÑ:LÐRX×ReÑReÔ(fˆÔ%Ü#(§8¡8×#5Ñ#5´e·k±kÀ!³nÓ#EˆÕ r=   r‚   r  r!   rG   r:   r.  r  r’   r  r  r<  c                 ól  — |}| j                  |«      }| j                  ||||||
¬«      \  }}}|| j                  j                  «       |z  z   }|}| j	                  |«      }| j                  |«      }|�|d d …df   |z  }|| j                  j                  «       |z  z   }|f}|r||fz  }|	r||fz  }|S )N)r‚   rG   r  r  r’   r  r   )r¾   rY  rZ  r_   r¿   r»   r[  )rZ   r‚   r  r!   rG   r:   r.  r  r’   r  r  r<  rÅ   r¡   rÆ   s                  r;   ra   z(MllamaCrossAttentionDecoderLayer.forward¯  sì   € ð !ˆØ×,Ñ,¨]Ó;ˆà6:·o±oØ'Ø/Ø#9Ø)Ø/Ø)ð 7Fó 7
Ñ3ˆ�| ^ð ! 4×#<Ñ#<×#AÑ#AÓ#CÀmÑ#SÑSˆà ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆØ(Ð4Ø9º!¸Q¸$Ñ?À-ÑOˆMØ  4×#;Ñ#;×#@Ñ#@Ó#BÀ]Ñ#RÑRˆà Ð"ˆáØ˜�Ñ&ˆGáØ˜Ð(Ñ(ˆGàˆr=   )NNFFNN)rb   rc   rd   rç   r   r  rQ   r/   re   r   r   r  r   r0   ra   rf   rg   s   @r;   rV  rV     s
  ø„ ÙVð
FÐ/ð 
F¸Cð 
FÀDõ 
Fð& 48Ø*.Ø,1Ø$)Ø59Ø6:ñ*à—|‘|ð*ð !&§¡ð*ð $Ÿl™lð	*ð
 Ÿ™ð*ð (-¨U¯\©\¸5¿<¹<Ð-GÑ'Hð*ð ˜u×/Ñ/Ñ0ð*ð ! ™ð*ð $ D™>ð*ð ˜D‘>ð*ð ! ×!1Ñ!1Ñ2ð*ð & e§l¡lÑ3ð*ð 
ˆu�|‰|Ñ	÷*r=   rV  c                   ó^   ‡ — e Zd Zddefˆ fd„Z ej                  «       ed„ «       «       Zˆ xZ	S )ÚMllamaRotaryEmbeddingrL   c                 ó^  •— t         ‰| �  «        |j                  d   | _        |j                  | _        |j                  | _        || _        t        | j                     | _	        | j                  | j                  |«      \  }| _
        | j                  d|d¬«       | j                  | _        y )NÚ	rope_typeÚinv_freqF)Ú
persistent)rP   rQ   Úrope_scalingr`  Úmax_position_embeddingsÚmax_seq_len_cachedÚoriginal_max_seq_lenrL   r   Úrope_init_fnÚattention_scalingÚregister_bufferra  Úoriginal_inv_freq)rZ   rL   r!  ra  r[   s       €r;   rQ   zMllamaRotaryEmbedding.__init__Ý  s“   ø€ Ü‰ÑÔØ×,Ñ,¨[Ñ9ˆŒØ"(×"@Ñ"@ˆÔØ$*×$BÑ$BˆÔ!àˆŒÜ/°·±Ñ?ˆÔØ+/×+<Ñ+<¸T¿[¹[È&Ó+QÑ(ˆ�$Ô(Ø×Ñ˜Z¨¸eÐÔDØ!%§¡ˆÕr=   c                 ó0  — | j                   d d d …d f   j                  «       j                  |j                  d   dd«      }|d d …d d d …f   j                  «       }t	        |j
                  j                  t        «      r/|j
                  j                  dk7  r|j
                  j                  nd}t        j                  |d¬«      5  |j                  «       |j                  «       z  j                  dd«      }t        j                  ||fd¬	«      }|j                  «       | j                  z  }|j                  «       | j                  z  }	d d d «       j                  |j                   ¬
«      	j                  |j                   ¬
«      fS # 1 sw Y   ŒAxY w)Nr   r(   r   ÚmpsÚcpuF)Údevice_typeÚenabledrk   r&   )r#   )ra  Úfloatr5  r)   Ú
isinstancer!  r"  Ústrr/   ÚautocastrE   r$  r,  rh  r-  r-   r#   )
rZ   r%  r.  Úinv_freq_expandedÚposition_ids_expandedrn  ÚfreqsÚembr,  r-  s
             r;   ra   zMllamaRotaryEmbedding.forwardé  sD  € ð !ŸM™M¨$²°4¨-Ñ8×>Ñ>Ó@×GÑGÈ×HZÑHZÐ[\ÑH]Ð_aÐcdÓeÐØ ,ªQ°²a¨ZÑ 8× >Ñ >Ó @Ðä'1°!·(±(·-±-ÄÔ'EÈ!Ï(É(Ï-É-Ð[`ÒJ`�a—h‘h—m’mÐfkˆÜ�^‰^¨¸UÔCñ 	5Ø&×,Ñ,Ó.Ð1F×1LÑ1LÓ1NÑN×YÑYÐZ[Ð]^Ó_ˆEÜ—)‘)˜U E˜N°Ô3ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆC÷		5ð �v‰v˜AŸG™GˆvÓ$ c§f¡f°1·7±7 fÓ&;Ð;Ð;÷	5ð 	5ús   ÃBFÆFrz   )
rb   rc   rd   r   rQ   r/   Úno_gradr   ra   rf   rg   s   @r;   r^  r^  Ü  s4   ø„ ñ
/Ð/õ 
/ð €U‡]�]ƒ_Øñ<ó ó ô<r=   r^  c                   ó  — e Zd ZeZdZdZg d¢ZdZdZ	dZ
dZd„ Z	 ddej                  dej                  dej                  d	ed
ef
d„Zedej                  dededej(                  dej*                  dej                  defd„«       Zy)ÚMllamaPreTrainedModelÚmodelT)r²   rV  rN  Fc                 óê  — | j                   j                  «       j                  }t        |t        j
                  t        j                  f«      rY|j                  j                  j                  d|¬«       |j                  �%|j                  j                  j                  «        y y t        |t        j                  «      rf|j                  j                  j                  d|¬«       |j                  �2|j                  j                  |j                     j                  «        y y t        |t        j                  «      r|j                  j                  d|¬«       y t        |t        «      r6t        j                   j                  |j"                  j                  |¬«       y t        |t$        «      r6t        j                   j                  |j&                  j                  |¬«       y t        |t(        «      rx|j*                  rkt        j                   j                  |j,                  j                  |¬«       t        j                   j                  |j.                  j                  |¬«       y y y )Nr  )rð   Ústd)r}  )rL   Úget_text_configÚinitializer_rangerq  r   r}   ÚConv2drë   ÚdataÚnormal_r‰   Úzero_rT   Úpadding_idxrV   ÚMllamaVisionModelÚinitÚclass_embeddingri   rU   r²   rM   rÂ   rÃ   )rZ   Úmoduler}  s      r;   Ú_init_weightsz#MllamaPreTrainedModel._init_weights  s£  € Ø�k‰k×)Ñ)Ó+×=Ñ=ˆÜ�fœrŸy™y¬"¯)©)Ð4Ô5Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜¤§¡Ô-Ø�K‰K×Ñ S¨cÐÕ2Ü˜Ô 1Ô2Ü�G‰G�O‰O˜F×2Ñ2×7Ñ7¸SˆOÕAÜ˜Ô BÔCÜ�G‰G�O‰O˜F×,Ñ,×1Ñ1°sˆOÕ;Ü˜Ô 8Ô9¸f¿oºoÜ�G‰G�O‰O˜F×,Ñ,×1Ñ1°sˆOÔ;Ü�G‰G�O‰O˜FŸO™O×0Ñ0°cˆOÕ:ð ?NÐ9r=   rG   Úinput_tensorr  Úpast_key_valuesr’   c           
      óÆ  — | j                   j                  dk(  r|�|dk(  j                  «       r|S y | j                   j                  dk(  r7t        |t        j
                  «      rt        |«      }t        |t        «      r|S |�|j                  «       nd}t        |t        «      }| j                   j                  dk(  r(|s&|s$t        j                  |||| j                  ¬«      ry |j                  |j                  }	}|j                  d   }
|r|j!                  «       }n1t        |t        j
                  «      r|j                  d   n||
z   dz   }| j#                  ||
|||	||j                  d   ¬	«      }| j                   j                  dk(  rQ|�O|j                  j$                  d
v r7|s5t	        j&                  |«      j(                  }t        j*                  ||«      }|S )NÚflash_attention_2r  Úflex_attentionr   r°   )Úinputs_embedsÚpast_key_values_lengthÚis_trainingr   r(   )Úsequence_lengthr@   r#   r!  r  r5   )r  Úxpu)rL   r¹   r3   rq  r/   re   r    r   Úget_seq_lengthr   r   Ú_ignore_causal_mask_sdparÞ   r#   r!  r)   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionr"  r1   r2   Ú_unmask_unattended)rZ   rG   rŠ  r  r‹  r’   Úpast_seen_tokensÚusing_static_cacher#   r!  r’  r@   r¢   Ú	min_dtypes                 r;   Ú_update_causal_maskz)MllamaPreTrainedModel._update_causal_mask  sÖ  € ð �;‰;×+Ñ+Ð/BÒBØÐ)¨~ÀÑ/D×.IÑ.IÔ.KØ%Ð%ØØ�;‰;×+Ñ+Ð/?Ò?Ü˜.¬%¯,©,Ô7Ü!<¸^Ó!L�Ü˜.¬)Ô4Ø%Ð%ð
 @OÐ?Z˜?×9Ñ9Ô;Ð`aÐÜ'¨¼ÓEÐð �;‰;×+Ñ+¨vÒ5Ñ>PÑYjÜ%×>Ñ>ØØ*Ø'7Ø ŸM™Mõ	ð à$×*Ñ*¨L×,?Ñ,?ˆvˆØ&×,Ñ,¨QÑ/ˆÙØ+×?Ñ?ÓA‰Mô ˜n¬e¯l©lÔ;ð ×$Ñ$ RÒ(à%¨Ñ7¸!Ñ;ð ð ×PÑPØØ+Ø'ØØØ)Ø#×)Ñ)¨!Ñ,ð Qó 
ˆð �K‰K×,Ñ,°Ò6ØÐ*Ø×%Ñ%×*Ñ*¨oÑ=Ù%ô
 Ÿ™ EÓ*×.Ñ.ˆIÜ0×CÑCÀKÐQZÓ[ˆKàÐr=   r’  r@   r#   r!  r5   c                 ó˜  — | �| j                  «       dk(  r| }|S t        j                  |«      j                  }	t        j                  ||f|	||¬«      }|dk7  rt        j
                  |d¬«      }|t        j                  ||¬«      |j                  dd«      kD  z  }|dddd…dd…f   j                  |ddd«      }| �Œ|j                  «       }| j                  d   }
|dd…dd…dd…d|
…f   | dd…dddd…f   j                  |j                  «      z   }|dk(  }|dd…dd…dd…d|
…f   j                  ||	«      |dd…dd…dd…d|
…f<   |S )	a°  
        Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
        `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

        Args:
            attention_mask (`torch.Tensor`):
                A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
                `(batch_size, 1, query_length, key_value_length)`.
            sequence_length (`int`):
                The sequence length being processed.
            target_length (`int`):
                The target length: when generating with static cache, the mask should be as long as the static cache,
                to account for the 0 padding, the part of the cache that is not filled yet.
            dtype (`torch.dtype`):
                The dtype to use for the 4D attention mask.
            device (`torch.device`):
                The device to place the 4D attention mask on.
            cache_position (`torch.Tensor`):
                Indices depicting the position of the input sequence tokens in the sequence.
            batch_size (`torch.Tensor`):
                Batch size.
        Nr¶   )Ú
fill_valuer#   r!  r   )Údiagonal©r!  r(   r   )r'   r/   r1   r2   ÚfullÚtriuÚarangerD   r5  Úcloner)   r-   r!  r.   )rG   r’  r@   r#   r!  r  r5   r?  r¢   r›  Úmask_lengthÚpadding_masks               r;   r—  zKMllamaPreTrainedModel._prepare_4d_causal_attention_mask_with_cache_positionb  sy  € ðD Ð%¨.×*<Ñ*<Ó*>À!Ò*Cà(ˆKð* Ðô' Ÿ™ EÓ*×.Ñ.ˆIÜŸ*™*Ø  -Ð0¸YÈeÐ\bôˆKð  !Ò#Ü#Ÿj™j¨¸qÔA�Øœ5Ÿ<™<¨¸fÔEÈ×H^ÑH^Ð_aÐcdÓHeÑeÑeˆKØ% d¨D²!²QÐ&6Ñ7×>Ñ>¸zÈ1ÈbÐRTÓUˆKØÐ)Ø)×/Ñ/Ó1�Ø,×2Ñ2°2Ñ6�Ø*ª1ªa²°L°[°LÐ+@ÑAÀNÒSTÐVZÐ\`ÒbcÐScÑDd×DgÑDgØ×&Ñ&óEñ  �ð  ,¨qÑ0�Ø5@ÂÂAÂqÈ,È;È,ÐAVÑ5W×5cÑ5cØ  )ó6�šAšq¢! \ k \Ð1Ñ2ð Ðr=   NrÇ   )rb   rc   rd   r   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_cache_classÚ_supports_static_cacheÚ_supports_sdpaÚ_supports_quantized_cacher‰  r/   re   r   r0   rœ  Ústaticmethodr  r#   r!  r—  rÓ   r=   r;   rz  rz  ù  sì   „ Ø€LØÐØ&*Ð#òÐð
 !ÐØ"ÐØ€NØ $Ðò;ð6 #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ñ7r=   rz  aK  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`MllamaConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, max_num_images, max_num_tiles, channels, image_size, image_size)):
            The tensors corresponding to the input images. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`MllamaImageProcessor.__call__`] for details ([]`MllamaProcessor`] uses
            [`MllamaImageProcessor`] for processing images).
        aspect_ratio_mask (`torch.Tensor` of shape `(batch_size, max_num_images, max_num_tiles)`, *optional*):
            Mask to avoid performing attention on padding tiles. Mask values selected in `[0, 1]`:

            - 1 for tiles that are **not masked**,
            - 0 for tiles that are **masked**.
        aspect_ratio_ids (`torch.Tensor` of shape `(batch_size, max_num_images)`, *optional*):
            Aspect ratio ids used to select the appropriate precomputed tile embeddings based on the aspect ratio of each input image.
            These ids correspond to indices in the model's list of supported aspect ratios, offset by 1.

            For example, if the model supports aspect ratios [[1, 1], [1, 2], [2, 1]]:
            - An image with aspect ratio [1, 1] would have ID 1
            - An image with aspect ratio [1, 2] would have ID 2
            - An image with aspect ratio [2, 1] would have ID 3

            The id 0 is reserved for padding (i.e., no image).

            If an image has aspect ratio [1, 2], that means it was split into 2 tiles horizontally, and its `aspect_ratio_id` would be 2.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        cross_attention_mask (`torch.Tensor` of shape `(batch_size, seq_length, max_num_images, max_num_tiles)`, *optional*):
            Cross-attention mask to control the interaction between text tokens and image tiles.
            This 4D tensor defines which image tiles each text token should attend to.

            For each text token (in seq_length):
            - 1 indicates the token **should attend** to the corresponding image tile
            - 0 indicates the token **should not attend** to the corresponding image tile
        cross_attention_states (`torch.FloatTensor`, *optional*):
            Output of the vision model, used for cross-attention. This tensor contains the processed image features that
            the language model will attend to.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
            Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
            blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
            returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

            Two formats are allowed:
            - a [`~cache_utils.Cache`] instance, see our
            [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache);
            - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
            shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
            cache format.

            The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
            legacy cache format will be returned.

            If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
            have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
            of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        pixel_values (`torch.FloatTensor` of shape `(batch_size, max_num_images, max_num_tiles, channels, image_size, image_size)):
            The tensors corresponding to the input images. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`MllamaImageProcessor.__call__`] for details ([]`MllamaProcessor`] uses
            [`MllamaImageProcessor`] for processing images).
        aspect_ratio_mask (`torch.Tensor` of shape `(batch_size, max_num_images, max_num_tiles)`, *optional*):
            Mask to avoid performing attention on padding tiles. Mask values selected in `[0, 1]`:

            - 1 for tiles that are **not masked**,
            - 0 for tiles that are **masked**.
        aspect_ratio_ids (`torch.Tensor` of shape `(batch_size, max_num_images)`, *optional*):
            Aspect ratio ids used to select the appropriate precomputed tile embeddings based on the aspect ratio of each input image.
            These ids correspond to indices in the model's list of supported aspect ratios, offset by 1.

            For example, if the model supports aspect ratios [[1, 1], [1, 2], [2, 1]]:
            - An image with aspect ratio [1, 1] would have ID 1
            - An image with aspect ratio [1, 2] would have ID 2
            - An image with aspect ratio [2, 1] would have ID 3

            The id 0 is reserved for padding (i.e., no image).

            If an image has aspect ratio [1, 2], that means it was split into 2 tiles horizontally, and its `aspect_ratio_id` would be 2.
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        cross_attention_mask (`torch.Tensor` of shape `(batch_size, seq_length, max_num_images, max_num_tiles)`, *optional*):
            Cross-attention mask to control the interaction between text tokens and image tiles.
            This 4D tensor defines which image tiles each text token should attend to.

            For each text token (in seq_length):
            - 1 indicates the token **should attend** to the corresponding image tile
            - 0 indicates the token **should not attend** to the corresponding image tile
        cross_attention_states (`torch.FloatTensor`, *optional*):
            Output of the vision model, used for cross-attention. This tensor contains the processed image features that
            the language model will attend to.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
            Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
            blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
            returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

            Two formats are allowed:
            - a [`~cache_utils.Cache`] instance, see our
            [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache);
            - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
            shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
            cache format.

            The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
            legacy cache format will be returned.

            If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
            have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
            of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
z>The Mllama Vision Model which consists of two vision encoders.c                   ó<  ‡ — e Zd ZeZdZdefˆ fd„Zd„ Zdej                  dej                  fd„Z
 ee«       eed¬	«      	 	 	 dd
ej                  dej                  dej                  dee   dee   dee   deeeej                  df   f   fd„«       «       Zˆ xZS )r…  Úvision_modelrL   c                 ó  •— t         ‰| �  |«       |j                  | _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        | j                  | j                  z  dz  dz   | _        |j
                  dz  | _	        t        j                  |j                  | j
                  | j                  | j                  dd¬«      | _        t        j                  | j                  t        j                  | j
                  «      z  «      | _        t#        |«      | _        t'        |d¬«      | _        t'        |d¬«      | _        t        j,                  | j
                  «      | _        t        j,                  | j
                  «      | _        t3        ||j4                  d¬«      | _        t3        ||j8                  d¬«      | _        | j=                  «        y )	Nrk   r   rl   ÚvalidF)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr‰   T)rM   )rP   rQ   rm   rn   rF   rR   Únum_channelsÚintermediate_layers_indicesr?   ro   r   r€  Úpatch_embeddingrV   r/   rp   r‡  ri   Úgated_positional_embeddingrK   Úpre_tile_positional_embeddingÚpost_tile_positional_embeddingr¼   Úlayernorm_preÚlayernorm_postrÉ   Únum_hidden_layersÚtransformerÚnum_global_layersÚglobal_transformerÚ	post_initr�   s     €r;   rQ   zMllamaVisionModel.__init__˜  s�  ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒØ ×+Ñ+ˆŒØ#×1Ñ1ˆÔØ!×-Ñ-ˆÔØ"×/Ñ/ˆÔØ+1×+MÑ+MˆÔ(à ŸO™O¨t¯©Ñ>À1ÑDÀqÑHˆÔØ×'Ñ'¨Ñ-ˆŒ
ä!Ÿy™yØ×+Ñ+Ø×)Ñ)ØŸ™Ø—?‘?ØØô 
ˆÔô  "Ÿ|™|¨D¯J©J¼¿¹ÀT×EUÑEUÓ9VÑ,VÓWˆÔÜ*LÈVÓ*TˆÔ'ä-RÐSYÐdhÔ-iˆÔ*Ü.SÐTZÐeiÔ.jˆÔ+ô  Ÿ\™\¨$×*:Ñ*:Ó;ˆÔÜ Ÿl™l¨4×+;Ñ+;Ó<ˆÔô /¨v°v×7OÑ7OÐZ_Ô`ˆÔÜ"5°f¸f×>VÑ>VÐaeÔ"fˆÔà�‰Õr=   c                 ó   — | j                   S )zg
        This function is used to fetch the first embedding layer to activate grads on inputs.
        )r»  rõ   s    r;   Úget_input_embeddingsz&MllamaVisionModel.get_input_embeddings½  s   € ð ×#Ñ#Ð#r=   r\   r$   c                 ó’   — |j                   \  }}}| j                  j                  |d|«      }t        j                  ||gd¬«      }|S )Nr   r&   )r)   r‡  r5  r/   r$  )rZ   r\   r5   r7   rR   r‡  s         r;   Úapply_class_embeddingz'MllamaVisionModel.apply_class_embeddingÃ  sI   € Ø%1×%7Ñ%7Ñ"ˆ
�A�{Ø×.Ñ.×5Ñ5°jÀ!À[ÓQˆÜ—y‘y /°<Ð!@ÀaÔHˆØÐr=   r   ©Úoutput_typer§  Úpixel_valuesr]   r>   r’   rÐ   rÑ   .c                 óH  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|j                  \  }}}	}
}}|j                  ||z  |	z  |
||«      }|j                  ||z  d«      }| j                  j                  j                  }| j                  j                  j                  }| j                  |j                  ||«      «      }|j                  d«      j                  dd«      }|j                  \  }}}|j                  ||z  |	d|«      }| j                  ||«      }|j                  ||z  |	z  ||«      }| j                  |«      }|dz  }|j                  ||z  |	||«      }| j                  ||«      }| j!                  |«      }d|j                  d   dz  z
  dz  }ddd|f}t#        j$                  ||dd¬	«      }|dkD  r| nd}|j                  ||z  d«      }t'        || j(                  |j                  d   | j                  ¬
«      }|j+                  ||z  d|«      }| j-                  ||d|¬«      }|d   }| j/                  |«      }|j                  ||z  |	||z   |«      }| j1                  ||«      }|j                  ||z  |	||z   z  |«      }| j3                  ||||¬«      }|d   }|j                  ||z  |	||z   |«      }|dd…dd…d|…f   }|j                  |||	||«      }| j4                  D �cg c]
  }|d   |   ‘Œ }}t7        j8                  |d¬«      }|j                  ||z  |	||z   d«      }|dd…dd…d|…f   }|j                  |||	|d«      }t7        j:                  ||gd¬«      }|rt=        |«      t=        |d   «      z   }nd}|r0|rt=        |d   «      nt=        |d   «      }t=        |d   «      |z   }nd}|st=        d„ |||fD «       «      S t?        |||¬«      S c c}w )aà  

        Returns:

        Example:

        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, MllamaVisionModel

        >>> checkpoint = "meta-llama/Llama-3.2-11B-Vision"
        >>> model = MllamaVisionModel.from_pretrained(checkpoint)
        >>> processor = AutoProcessor.from_pretrained(checkpoint)

        >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> inputs = processor(images=image, return_tensors="pt")

        >>> output = model(**inputs)

        >>> print(output.last_hidden_state.shape)
        torch.Size([1, 1, 4, 1025, 7680])
        ```
        Nr(   rk   r   é   rB   r   Úconstant)Úmoderž   )r>   r?   r@   r#   T)rG   rÐ   r’   r&   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrz   rÓ   rÕ   s     r;   rØ   z,MllamaVisionModel.forward.<locals>.<genexpr>_  s   è ø€ Ò_˜qÐQRÑQ^œÑ_ùrÙ   rÚ   ) rL   r’   rÐ   rÝ   r)   rD   r»  rë   r#   r!  r-   ÚflattenrE   r½  rÉ  r¼  r¿  r­   ÚpadrI   r?   r+   rÂ  rÀ  r¾  rÄ  rº  r/   Ústackr$  rá   r   ) rZ   rÌ  r]   r>   r’   rÐ   rÑ   r5   Únum_concurrent_mediaÚ	num_tilesr¹  ÚheightÚwidthÚtarget_dtypeÚtarget_deviceÚpatch_embedsr\   r7   r?   r'   Únum_padding_patchesr¸  Úslice_indexrG   r¤   Úglobal_outputÚiÚall_intermediate_hidden_statesÚintermediate_hidden_statesr‚   Úglobal_attnrÜ   s                                    r;   ra   zMllamaVisionModel.forwardÉ  s  € ðH 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàS_×SeÑSeÑPˆ
Ð(¨)°\À6È5à#×+Ñ+¨JÐ9MÑ,MÐPYÑ,YÐ[gÐioÐqvÓwˆØ+×3Ñ3°JÐAUÑ4UÐWYÓZÐð ×+Ñ+×2Ñ2×8Ñ8ˆØ×,Ñ,×3Ñ3×:Ñ:ˆØ×+Ñ+¨L¯O©O¸MÈ<Ó,XÓYˆØ#×+Ñ+¨AÓ.×8Ñ8¸¸AÓ>ˆð +×0Ñ0Ñˆˆ;˜Ø#×+Ñ+¨JÐ9MÑ,MÈyÐZ\Ð^aÓbˆØ×9Ñ9¸,ÐHXÓYˆð $×+Ñ+¨JÐ9MÑ,MÐPYÑ,YÐ[fÐhkÓlˆØ×1Ñ1°,Ó?ˆØ�qÑˆð $×+Ñ+¨JÐ9MÑ,MÈyÐZeÐgjÓkˆØ×6Ñ6°|ÐEUÓVˆà×)Ñ)¨,Ó7ˆð  ! L×$6Ñ$6°rÑ$:¸QÑ$>Ñ?À1ÑDÐà�a˜Ð/Ð0ˆä—u‘u˜\¨7¸È1ÔMˆØ.AÀAÒ.EÐ*Ñ*È4ˆð +×2Ñ2°:Ð@TÑ3TÐVXÓYˆÜ=Ø,Ø×(Ñ(Ø&×,Ñ,¨QÑ/Ø—*‘*ô	
ˆð $×(Ñ(¨Ð6JÑ)JÈBÐPSÓTˆØ×!Ñ!ØØ)Ø!%Ø/ð	 "ó 
ˆð ˜a‘yˆà×*Ñ*¨<Ó8ˆð $×+Ñ+ØÐ-Ñ-¨y¸+ÐH[Ñ:[Ð]`ó
ˆð ×:Ñ:¸<ÐIYÓZˆØ#×+Ñ+ØÐ-Ñ-¨y¸KÐJ]Ñ<]Ñ/^Ð`có
ˆð ×/Ñ/ØØ)Ø!5Ø/ð	 0ó 
ˆð % QÑ'ˆð $×+Ñ+ØÐ-Ñ-¨y¸+ÐH[Ñ:[Ð]`ó
ˆð $¢A¢q¨,¨;¨,Ð$6Ñ7ˆØ#×+Ñ+¨JÐ8LÈiÐYdÐfiÓjˆð AE×@`Ñ@`Ö)a¸1¨&°©)°A«,Ð)aÐ&Ð)aÜ%*§[¡[Ð1OÐUWÔ%XÐ"ð &@×%GÑ%GØÐ-Ñ-¨y¸+ÐH[Ñ:[Ð]_ó&
Ð"ð &@ÂÂ1ÀlÀ{ÀlÐ@RÑ%SÐ"Ø%?×%GÑ%GØÐ,¨i¸Àbó&
Ð"ô
 —y‘y ,Ð0JÐ!KÐQSÔTˆáÜ!Ð"@ÓAÄEÈ-ÐXYÑJZÓD[Ñ[‰Mà ˆMáá5Iœ% ¨aÑ 0Ô1ÌuÐUbÐcdÑUeÓOfˆKÜ˜v a™yÓ)¨KÑ7‰JàˆJáÜÑ_ \°=À*Ð$MÔ_Ó_Ð_äØ*Ø'Ø!ô
ð 	
ùò= *bs   Ì)P)NNN)rb   rc   rd   r   r§  r¨  rQ   rÇ  r/   re   rÉ  r   ÚMLLAMA_VISION_INPUTS_DOCSTRINGr   r   r   r0   r   r   ra   rf   rg   s   @r;   r…  r…  �  sò   ø„ ð
 &€LØ&Ðð#Ð1õ #òJ$ð°%·,±,ð À5Ç<Á<ó ñ +Ð+IÓJÙ¨?ÐI]Ô^ð -1Ø/3Ø&*ñZ
à—l‘lðZ
ð  Ÿ,™,ðZ
ð !Ÿ<™<ð	Z
ð
 $ D™>ðZ
ð ' t™nðZ
ð ˜d‘^ðZ
ð 
ˆ  e§l¡l°CÐ&7Ñ 8Ð8Ñ	9òZ
ó _ó KôZ
r=   r…  zYThe Mllama Text Model which consists of transformer with self and cross attention layers.c            !       óþ  ‡ — e Zd ZeZdZdefˆ fd„Zd„ Zd„ Z e	e
«       eed¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     d	eej                      d
eej                     deej"                     deej                      deeej                   ej                   f      deeeeej"                     f      deej"                     dee   dee   dee   dee   deej                     deeef   fd„«       «       Zˆ xZS )ÚMllamaTextModelzlanguage_model.modelrL   c                 ó–  •— t         ‰| �  |«       |j                  | _        |j                  | _        t        j                  |j                  dz   |j                  | j                  «      | _        |j                  | _	        g }t        |j                  «      D ]G  }|| j                  v r|j                  t        ||«      «       Œ-|j                  t        ||«      «       ŒI t        j                  |«      | _        t#        |j                  |j$                  ¬«      | _        t)        |¬«      | _        d| _        | j/                  «        y )NrÎ  r´   )rL   F)rP   rQ   Úpad_token_idr„  Ú
vocab_sizer   rT   rR   Úembed_tokensÚcross_attention_layersrÌ   rÁ  ÚappendrV  rN  rË   rÍ   ré   rþ   Únormr^  Ú
rotary_embrÎ   rÅ  )rZ   rL   rÍ   rù   r[   s       €r;   rQ   zMllamaTextModel.__init__p  s  ø€ Ü‰Ñ˜Ô Ø!×.Ñ.ˆÔØ ×+Ñ+ˆŒÜŸL™L¨×):Ñ):¸QÑ)>À×@RÑ@RÐTX×TdÑTdÓeˆÔØ&,×&CÑ&CˆÔ#àˆÜ˜v×7Ñ7Ó8ò 	RˆIØ˜D×7Ñ7Ñ7Ø—‘Ô>¸vÀyÓQÕRà—‘Ô=¸fÀiÓPÕQð		Rô —m‘m FÓ+ˆŒÜ% f×&8Ñ&8¸f×>QÑ>QÔRˆŒ	Ü/°vÔ>ˆŒØ&+ˆÔ#Ø�‰Õr=   c                 ó   — | j                   S rz   ©ré  rõ   s    r;   rÇ  z$MllamaTextModel.get_input_embeddings„  s   € Ø× Ñ Ð r=   c                 ó   — || _         y rz   rï  ©rZ   rž   s     r;   Úset_input_embeddingsz$MllamaTextModel.set_input_embeddings‡  s
   € Ø!ˆÕr=   r   rÊ  Ú	input_idsrG   r.  r  r!   r:   r‹  r�  r  r’   rÐ   rÑ   r  r$   c                 óÞ  — |
�|
n| j                   j                  }
|�|n| j                   j                  }|	�|	n| j                   j                  }	|�|n| j                   j                  }|du |duz  rt        d«      ‚| j                  r%| j                  r|	rt        j                  d«       d}	|€| j                  |«      }|}|	r|€
t        «       }|€F|�|j                  «       nd}t        j                  |||j                  d   z   |j                   ¬«      }|€|j#                  d«      }| j%                  |||||
«      }| j'                  ||«      }|rdnd}|
rdnd}d}t)        | j*                  «      D ]°  \  }}|r||fz  }|| j,                  v }|du xs |duxr |j                  |«      dk(  }|r|€|rŒC| j                  r3| j                  r'| j/                  |j0                  ||||||||
|	||«      }n |||||||||
|	||¬	«      }|d   }|	r	||
rd
nd   }|
sŒ¨||d   fz  }Œ² | j3                  |«      }|r||fz  }|	r|nd}|st5        d„ ||||fD «       «      S t7        ||||¬«      S )aN  

        Returns:

        Example:

        ```python
        >>> from transformers import AutoProcessor, MllamaTextModel

        >>> checkpoint = "meta-llama/Llama-3.2-11B-Vision"
        >>> model = MllamaTextModel.from_pretrained(checkpoint)
        >>> processor = AutoProcessor.from_pretrained(checkpoint)

        >>> text = "<|image|>If I had to write a haiku for this one"
        >>> inputs = processor(text=text, return_tensors="pt")

        >>> output = model(**inputs)

        >>> print(output.last_hidden_state.shape)
        torch.Size([1, 13, 4096])
        ```
        Nú:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.Fr   r   r   rÓ   )
r  r!   rG   r:   r.  r  r’   r  r  r<  rk   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrz   rÓ   rÕ   s     r;   rØ   z*MllamaTextModel.forward.<locals>.<genexpr>  s   è ø€ Òt˜qÐfgÑfsœÑtùrÙ   )rÛ   r‹  r‚   rÜ   )rL   r’   rÐ   r  rÝ   r  rÎ   rÞ   r«   r¬   ré  r   r”  r/   r£  r)   r!  r,   rœ  rí  Ú	enumeraterÍ   rê  rß   rà   rì  rá   r   )rZ   ró  rG   r.  r  r!   r:   r‹  r�  r  r’   rÐ   rÑ   r  r‚   r™  r¢   r<  Úall_hidden_statesÚall_self_attnsÚnext_decoder_cacheÚidxÚdecoder_layerÚis_cross_attention_layerÚis_cross_attention_cache_emptyrå   Ú
next_caches                              r;   ra   zMllamaTextModel.forwardŠ  s  € ðP 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø%0Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà˜Ð -°tÐ";Ò<ÜÐYÓZÐZà×&Ò&¨4¯=ª=¹YÜ×ÑØjôð ˆIàÐ Ø ×-Ñ-¨iÓ8ˆMà%ˆá˜Ð0Ü*›nˆOàÐ!ØCRÐC^˜×=Ñ=Ô?ÐdeÐÜ"Ÿ\™\Ø Ð"2°]×5HÑ5HÈÑ5KÑ"KÐTa×ThÑThôˆNð ÐØ)×3Ñ3°AÓ6ˆLà×.Ñ.Ø˜M¨>¸?ÐL]ó
ˆð
 #Ÿo™o¨m¸\ÓJÐñ #7™B¸DÐÙ0™°dˆØ!Ðä"+¨D¯K©KÓ"8ò 3	6ÑˆC�Ù#Ø! mÐ%5Ñ5Ð!ð
 (+¨d×.IÑ.IÐ'IÐ$Ø-<ÀÐ-Dò .Ø tÐ+ÒX°×0NÑ0NÈsÓ0SÐWXÑ0Xð +ñ (Ð,BÐ,JÑOmØà×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø*Ø(ØØ1Ø Ø#Ø%ØØ"Ø'ó!‘ñ !.Ø!Ø+AØ)=Ø#.Ø2OØ!-Ø#2Ø&7Ø'Ø#1Ø(;ô!�ð *¨!Ñ,ˆMáØ%2Ñ8I±1ÈqÑ%QÐ"â Ø =°Ñ#3Ð"5Ñ5‘ðg3	6ðj Ÿ	™	 -Ó0ˆñ  Ø -Ð!1Ñ1Ðá+4Ñ'¸$ˆ
áÜÑt ]°JÐ@QÐSaÐ$bÔtÓtÐtÜ&Ø+Ø&Ø+Ø%ô	
ð 	
r=   )NNNNNNNNNNNNN)rb   rc   rd   r   r§  r¨  rQ   rÇ  rò  r   ÚMLLAMA_TEXT_INPUTS_DOCSTRINGr   r   r   r/   r  re   rT  r   r   r   r   r0   ra   rf   rg   s   @r;   rå  rå  h  s¬  ø„ ð
 $€LØ.ÐðÐ/õ ò(!ò"ñ +Ð+GÓHÙÐ+BÐQcÔdð 15Ø15Ø37Ø>BØ7;ØUYØKOØ59Ø$(Ø,0Ø/3Ø&*Ø59ñV
à˜E×,Ñ,Ñ-ðV
ð ! §¡Ñ.ðV
ð ˜u×/Ñ/Ñ0ð	V
ð
 !)¨×):Ñ):Ñ ;ðV
ð ' u§|¡|Ñ4ðV
ð (0°°e·l±lÀEÇLÁLÐ6PÑ0QÑ'RðV
ð " %¨¨t°E×4EÑ4EÑ/FÐ(FÑ"GÑHðV
ð   × 1Ñ 1Ñ2ðV
ð ˜D‘>ðV
ð $ D™>ðV
ð ' t™nðV
ð ˜d‘^ðV
ð ! ×!1Ñ!1Ñ2ðV
ð 
ˆuÐ-Ð-Ñ	.òV
ó eó IôV
r=   rå  z;The Mllama Text Model with a language modeling head on top.c            &       óz  ‡ — e Zd ZeZdZdZdgZˆ fd„Zd„ Z	d„ Z
d„ Zd„ Zd	„ Zd
„ Z eddd¬«       ee«       eed¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!deej,                     deej.                     deej,                     deej,                     deej,                     deeej.                  ej.                  f      deeeeej8                     f      deej8                     deej,                     dee   dee   dee   dee   deej,                     deeej.                  f   deeef   f d „«       «       «       Zˆ xZ S )"ÚMllamaForCausalLMTÚlanguage_modelzlm_head.weightc                 ó~  •— t         ‰| �  |j                  «       «       |j                  «       | _        | j                  j                  | _        t
        j                  | j                  «      | _        t        j                  | j                  j                  | j                  d¬«      | _        | j                  «        y r‡   )rP   rQ   r~  Útext_configrè  rå  Ú_from_configr{  r   r}   rR   Úlm_headrÅ  r�   s     €r;   rQ   zMllamaForCausalLM.__init__/  s„   ø€ Ü‰Ñ˜×/Ñ/Ó1Ô2Ø!×1Ñ1Ó3ˆÔØ×*Ñ*×5Ñ5ˆŒÜ$×1Ñ1°$×2BÑ2BÓCˆŒ
Ü—y‘y ×!1Ñ!1×!=Ñ!=¸t¿¹ÐUZÔ[ˆŒà�‰Õr=   c                 ó.   — | j                   j                  S rz   ©r{  ré  rõ   s    r;   rÇ  z&MllamaForCausalLM.get_input_embeddings8  s   € Ø�z‰z×&Ñ&Ð&r=   c                 ó&   — || j                   _        y rz   r	  rñ  s     r;   rò  z&MllamaForCausalLM.set_input_embeddings;  s   € Ø"'ˆ�
‰
Õr=   c                 ó   — | j                   S rz   ©r  rõ   s    r;   Úget_output_embeddingsz'MllamaForCausalLM.get_output_embeddings>  s   € Ø�|‰|Ðr=   c                 ó   — || _         y rz   r  ©rZ   Únew_embeddingss     r;   Úset_output_embeddingsz'MllamaForCausalLM.set_output_embeddingsA  s	   € Ø%ˆ�r=   c                 ó   — || _         y rz   ©r{  ©rZ   Údecoders     r;   Úset_decoderzMllamaForCausalLM.set_decoderD  s	   € Øˆ�
r=   c                 ó   — | j                   S rz   r  rõ   s    r;   Úget_decoderzMllamaForCausalLM.get_decoderG  s   € Ø�z‰zÐr=   Únum_logits_to_keepú4.50Úlogits_to_keep©ÚversionÚnew_namer   rÊ  ró  rG   r.  r  r!   r:   r‹  r�  Úlabelsr  r’   rÐ   rÑ   r  r$   c                 ó@  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  |||||||||
||||¬«      }|d   }t        |t        «      rt        | d«      n|}| j                  |dd…|dd…f   «      j                  «       }d}|	� | j                  ||	| j                  fi |¤Ž}|s|f|dd z   }|�|f|z   S |S t        |||j                  |j                  |j                  ¬«      S )a¬  
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
                config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
                (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

            logits_to_keep (`int` or `torch.Tensor`, *optional*):
                If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
                `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
                token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
                If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
                This is useful when using packed tensor format (single dimension for batch and sequence length).

        Returns:

        Example:

        ```python
        >>> from transformers import AutoTokenizer, MllamaForCausalLM

        >>> model = MllamaForCausalLM.from_pretrained("Llama-3.2-11B-Vision")
        >>> tokenizer = AutoTokenizer.from_pretrained("Llama-3.2-11B-Vision")

        >>> prompt = "If I had to write a haiku, it would be:"
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=40, do_sample=True, temperature=0.6)
        >>> result = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        >>> print(result)
        If I had to write a haiku, it would be: "Snowflakes gently fall" - simple, yet peaceful.
        I love the idea of snowflakes gently falling, each one
        ```
        N)ró  r  rG   r.  r!   r:   r‹  r�  r  r’   rÐ   rÑ   r  r   r   ©ÚlossÚlogitsr‹  r‚   rÜ   )rL   r’   rÐ   rÝ   r{  rq  r  Úslicer  rp  Úloss_functionrè  r   r‹  r‚   rÜ   )rZ   ró  rG   r.  r  r!   r:   r‹  r�  r  r  r’   rÐ   rÑ   r  r  Úloss_kwargsrÆ   r‚   Úslice_indicesr#  r"  r¤   s                          r;   ra   zMllamaForCausalLM.forwardJ  s\  € ðp 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆð —*‘*ØØ#9Ø)Ø%Ø!5Ø*GØ+Ø'ØØ/Ø!5Ø#Ø)ð ó 
ˆð    ™
ˆÜ8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜mªA¨}ºaÐ,?Ñ@ÓA×GÑGÓIˆàˆØÐØ%�4×%Ñ% f¨f°d·o±oÑUÈÑUˆDáØ�Y ¨¨ Ñ,ˆFØ'+Ð'7�D�7˜VÑ#ÐC¸VÐCä%ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r=   )NNNNNNNNNNNNNNr   )!rb   rc   rd   r   r§  r¬  r¨  Ú_tied_weights_keysrQ   rÇ  rò  r  r  r  r  r   r   ÚMLLAMA_INPUTS_DOCSTRINGr   r   r   r/   r  re   r   r   r   r   rT  r0   r  ra   rf   rg   s   @r;   r  r  %  s  ø„ ð
 $€LØ!ÐØ(ÐØ*Ð+Ðôò'ò(òò&òòñ Ð)°6ÐDTÔUÙ*Ð+BÓCÙÐ+AÐPbÔcð 15Ø15Ø37Ø=AØ;?ØUYØKOØ59Ø-1Ø$(Ø,0Ø/3Ø&*Ø59Ø34ñ!^
à˜E×,Ñ,Ñ-ð^
ð ! §¡Ñ.ð^
ð ˜u×/Ñ/Ñ0ð	^
ð
 !)¨×)9Ñ)9Ñ :ð^
ð ' u×'7Ñ'7Ñ8ð^
ð (0°°e·l±lÀEÇLÁLÐ6PÑ0QÑ'Rð^
ð " %¨¨t°E×4EÑ4EÑ/FÐ(FÑ"GÑHð^
ð   × 1Ñ 1Ñ2ð^
ð ˜×)Ñ)Ñ*ð^
ð ˜D‘>ð^
ð $ D™>ð^
ð ' t™nð^
ð ˜d‘^ð^
ð ! ×!1Ñ!1Ñ2ð^
ð  ˜c 5§<¡<Ð/Ñ0ð!^
ð$ 
ˆuÐ,Ð,Ñ	-ò%^
ó dó Dó Vô^
r=   r  zIThe Mllama model which consists of a vision encoder and a language model.c            *       ó¾  ‡ — e Zd ZdZdefˆ fd„Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Z ed
dd¬«       ee«       eed¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$deej&                     deej(                     deej*                     deej*                     deej*                     deej*                     deej*                     deej&                     deeeeej(                     f      deej(                     deej&                     dee   dee   dee   dee   deej&                     deeej*                  f   d eeef   f$d!„«       «       «       Z	 	 	 	 	 	 	 	 	 	 	 	 d%d"„Zˆ fd#„Zˆ xZS )&ÚMllamaForConditionalGenerationFrL   c                 ó*  •— t         ‰| �  |«       |j                  j                  | _        |j                  j                  | _        |j
                  j                  | _        |j
                  j                  | _        | j                  j                  �| j                  j                  nd| _	        t        j                  |j
                  «      | _        t        j                  |j                  «      | _        | j                  j                  �,| j                  j                  D �cg c]  }d|› �‘Œ	 c}| _        t!        j"                  |j
                  j                  |j                  j                  d¬«      | _        | j'                  «        y c c}w )Nr(   zlanguage_model.Trˆ   )rP   rQ   r  rè  rR   Úvision_configrF   Úvision_output_dimrL   rç  r…  r  r±  r  r  r(  r   r}   Úmulti_modal_projectorrÅ  )rZ   rL   r+  r[   s      €r;   rQ   z'MllamaForConditionalGeneration.__init__µ  s1  ø€ Ü‰Ñ˜Ô Ø ×,Ñ,×7Ñ7ˆŒØ!×-Ñ-×9Ñ9ˆÔØ#×1Ñ1×?Ñ?ˆÔØ!'×!5Ñ!5×!GÑ!GˆÔØ8<¿¹×8PÑ8PÐ8\˜DŸK™K×4Ò4ÐbdˆÔä-×:Ñ:¸6×;OÑ;OÓPˆÔÜ/×<Ñ<¸V×=OÑ=OÓPˆÔØ×Ñ×1Ñ1Ð=ØFJ×FYÑFY×FlÑFlÖ&mÀ¨¸¸Ò'<Ò&mˆDÔ#ä%'§Y¡YØ× Ñ ×2Ñ2Ø×Ñ×*Ñ*Øô&
ˆÔ"ð
 	�‰Õùò 'ns   Ä&Fc                 ó6   — | j                   j                  «       S rz   )r  rÇ  rõ   s    r;   rÇ  z3MllamaForConditionalGeneration.get_input_embeddingsÉ  s   € Ø×"Ñ"×7Ñ7Ó9Ð9r=   c                 ó:   — | j                   j                  |«       y rz   )r  rò  rñ  s     r;   rò  z3MllamaForConditionalGeneration.set_input_embeddingsÌ  s   € Ø×Ñ×0Ñ0°Õ7r=   c                 ó6   — | j                   j                  «       S rz   )r  r  rõ   s    r;   r  z4MllamaForConditionalGeneration.get_output_embeddingsÏ  s   € Ø×"Ñ"×8Ñ8Ó:Ð:r=   c                 ó:   — | j                   j                  |«       y rz   )r  r  r  s     r;   r  z4MllamaForConditionalGeneration.set_output_embeddingsÒ  s   € Ø×Ñ×1Ñ1°.ÕAr=   c                 ó:   — | j                   j                  |«       y rz   )r  r  r  s     r;   r  z*MllamaForConditionalGeneration.set_decoderÕ  s   € Ø×Ñ×'Ñ'¨Õ0r=   c                 ó6   — | j                   j                  «       S rz   )r  r  rõ   s    r;   r  z*MllamaForConditionalGeneration.get_decoderØ  s   € Ø×"Ñ"×.Ñ.Ó0Ð0r=   r  r  r  r  r   rÊ  ró  rÌ  r>   r]   rG   r!   r  r.  r‹  r�  r  r  r’   rÐ   rÑ   r  r$   c                 ó´  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|du |
duz  rt	        d«      ‚|�|
�t	        d«      ‚|�|�t	        d«      ‚|�b|€t	        d«      ‚| j                  ||||||¬«      }|d   }| j                  |«      j                  d|j                  d	   | j                  «      }|�0t        || j
                  j                  | j                  ¬
«      \  }}nd}|�|�|dd…dd…|f   }|dd…dd…|f   } | j                  d|||||||	||
|||||dœ|¤Ž}d}|d   }|�7 | j                  ||| j                   j                  «       j                   fi |¤Ž}|s
|�|f|z   S |S t#        ||j$                  |j&                  |j(                  |j*                  ¬«      S )a´  
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
                config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
                (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

            logits_to_keep (`int` or `torch.Tensor`, *optional*):
                If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
                `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
                token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
                If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
                This is useful when using packed tensor format (single dimension for batch and sequence length).


        Returns:

        Example:

        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, MllamaForConditionalGeneration

        >>> checkpoint = "meta-llama/Llama-3.2-11B-Vision"
        >>> model = MllamaForConditionalGeneration.from_pretrained(checkpoint)
        >>> processor = AutoProcessor.from_pretrained(checkpoint)

        >>> prompt = "<|image|>If I had to write a haiku for this one"
        >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = processor(text=prompt, images=image, return_tensors="pt")

        >>> # Generate
        >>> output = model.generate(**inputs, max_new_tokens=15)

        >>> prompt_len = inputs.input_ids.shape[-1]
        >>> generated_ids = output[:, prompt_len:]
        >>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
        >>> print(generated_text)
        [', it would be:.\\nA stop sign in Chinatown.\\n']
        ```
        Nrõ  zdYou cannot specify both pixel_values and inputs_embeds at the same time, and must specify either onezM`pixel_values` and `cross_attention_states` cannot be provided simultaneouslyzA`aspect_ratio_ids` must be provided if `pixel_values` is provided)rÌ  r]   r>   rÐ   r’   rÑ   r   r(   rB   )r"   r#   )ró  rG   r.  r  r!   r:   r‹  r  r�  rÐ   r’   rÑ   r  r  r!  rÓ   )rL   r’   rÐ   rÝ   r  r±  r/  rD   r)   rR   r<   r?   r#   r  r%  r~  rè  r   r#  r‹  r‚   rÜ   )rZ   ró  rÌ  r>   r]   rG   r!   r  r.  r‹  r�  r  r  r’   rÐ   rÑ   r  r  r&  Úvision_outputsr:   rÆ   r"  r#  s                           r;   ra   z&MllamaForConditionalGeneration.forwardÛ  so  € ðF 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà˜Ð -°tÐ";Ò<ÜÐYÓZÐZàÐ#¨Ð(AÜØvóð ð Ð#Ð(>Ð(JÜÐlÓmÐmàÐ#ØÐ'Ü Ð!dÓeÐeà!×.Ñ.Ø)Ø!1Ø"3Ø%9Ø"3Ø'ð /ó ˆNð &4°AÑ%6Ð"Ø%)×%?Ñ%?Ð@VÓ%W×%_Ñ%_ØÐ*×0Ñ0°Ñ4°d×6FÑ6Fó&Ð"ð  Ð+ÜB_Ø$Ø"&×"3Ñ"3×"?Ñ"?Ø—j‘jôCÑ?Ð Ñ"?ð -1Ð)àÐ+°Ð0JØ#7ºº1¸nÐ8LÑ#MÐ Ø,IÊ!ÊQÐP^ÐJ^Ñ,_Ð)à%�$×%Ñ%ð 
ØØ)Ø%Ø#9Ø!5Ø*GØ+ØØ'Ø!5Ø/Ø#Ø)Ø)ñ
ð ñ
ˆð& ˆØ˜‘ˆàÐØ%�4×%Ñ% f¨f°d·k±k×6QÑ6QÓ6S×6^Ñ6^ÑnÐbmÑnˆDáØ(,Ð(8�D�7˜WÑ$ÐE¸gÐEä%ØØ—>‘>Ø#×3Ñ3Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r=   c           	      óz  — |	�Y|€|d   |j                   d   k\  r|d d …|j                   d    d …f   }n(|j                   d   |j                   d   k7  r	|d d …|f   }|�t|€r|j                  «       j                  d«      dz
  }|j                  |dk(  d«       |	r9|d d …|j                   d    d …f   }|j	                  t
        j                  ¬«      }|�|d   dk(  r|d dœ}n#|j	                  t
        j                  ¬«      d dœ}|�||d<   |j                  |||	|
||dœ«       |d   dk(  r||d	<   ||d
<   ||d<   |S )Nr(   r   r   )Úmemory_format)r�  ró  )ró  r�  r  )r.  r  r‹  r  rG   r!   rÌ  r]   r>   )r)   ÚlongÚcumsumÚmasked_fill_r¤  r/   Úcontiguous_formatr  )rZ   ró  r�  rG   r.  rÌ  r]   r>   r!   r‹  r  r  r  r?  Úmodel_inputss                  r;   Úprepare_inputs_for_generationz<MllamaForConditionalGeneration.prepare_inputs_for_generationq  s”  € ð. Ð&àÐ)Ø! "Ñ%¨¯©¸Ñ);Ò;à%¢a¨.×*>Ñ*>¸qÑ*AÐ)AÑ)CÐ&CÑD‘	Ø—‘ Ñ# ~×';Ñ';¸AÑ'>Ò>Ø%¢a¨Ð&7Ñ8�	ð Ð%¨,Ð*>à)×.Ñ.Ó0×7Ñ7¸Ó;¸aÑ?ˆLØ×%Ñ% n¸Ñ&9¸1Ô=ÙØ+ªA°	·±ÀÑ0BÐ/BÑ/DÐ,DÑE�ð  ,×1Ñ1Ä×@WÑ@WÐ1ÓX�ð Ð$¨¸Ñ):¸aÒ)?Ø-:ÈÑN‰Lð *3¯©Äu×G^ÑG^¨Ó)_ÐrvÑwˆLàÐ%Ø-;ˆLÐ)Ñ*à×Ñà ,Ø"0Ø#2Ø&Ø"0Ø(<ñô		
ð ˜!Ñ Ò!Ø+7ˆL˜Ñ(Ø/?ˆLÐ+Ñ,Ø0AˆLÐ,Ñ-àÐr=   c                 ó¢   •— |j                  dd «      }t        ‰| �  d|||dœ|¤Ž}|�&t        j                  ||d d …dd …df   gd¬«      |d<   |S )Nr!   )rÆ   Úmodel_kwargsÚis_encoder_decoderr(   .r   r&   rÓ   )ÚgetrP   Ú#_update_model_kwargs_for_generationr/   r$  )rZ   rÆ   rA  rB  r?  Úcross_attention_mask_prevr[   s         €r;   rD  zBMllamaForConditionalGeneration._update_model_kwargs_for_generationº  s{   ø€ Ø$0×$4Ñ$4Ð5KÈTÓ$RÐ!Ü‘wÑBð 
ØØ%Ø1ñ
ð ñ	
ˆð %Ð0Ü38·9±9Ø*Ð,EÂaÈÉÈcÀkÑ,RÐSÐYZô4ˆLÐ/Ñ0ð Ðr=   )NNNNNNNNNNNNNNNNr   )NNNNNNNNNFNN) rb   rc   rd   r®  r   rQ   rÇ  rò  r  r  r  r  r   r   r)  r   r   r   r/   r  rT  re   r   r   r   r0   r  r   ra   r?  rD  rf   rg   s   @r;   r+  r+  ®  s\  ø„ ð
 !&Ðð˜|õ ò(:ò8ò;òBò1ò1ñ Ð)°6ÐDTÔUÙ*Ð+BÓCÙÐ+AÐP^Ô_ð 15Ø48Ø48Ø37Ø15Ø7;Ø9=Ø37ØKOØ59Ø-1Ø$(Ø,0Ø/3Ø&*Ø59Ø34ñ%Q
à˜E×,Ñ,Ñ-ðQ
ð ˜u×0Ñ0Ñ1ðQ
ð $ E§L¡LÑ1ð	Q
ð
 # 5§<¡<Ñ0ðQ
ð ! §¡Ñ.ðQ
ð ' u§|¡|Ñ4ðQ
ð !)¨¯©Ñ 6ðQ
ð ˜u×/Ñ/Ñ0ðQ
ð " %¨¨t°E×4EÑ4EÑ/FÐ(FÑ"GÑHðQ
ð   × 1Ñ 1Ñ2ðQ
ð ˜×)Ñ)Ñ*ðQ
ð ˜D‘>ðQ
ð $ D™>ðQ
ð ' t™nðQ
ð  ˜d‘^ð!Q
ð" ! ×!1Ñ!1Ñ2ð#Q
ð$ ˜c 5§<¡<Ð/Ñ0ð%Q
ð( 
ˆuÐ,Ð,Ñ	-ò)Q
ó `ó Dó VðQ
ðj ØØØØØØØ!ØØØØóG÷Rð r=   r+  )r+  r  rå  r…  rz  rO   )Zrç   r–   Útypingr   r   r   r   r/   Útorch.nn.functionalr   r˜   r­   Útorch.utils.checkpointÚ r	   Úactivationsr
   Úcache_utilsr   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   Úmodeling_outputsr   r   r   Úmodeling_rope_utilsr   r   Úutilsr   r   r   r   r   Úutils.deprecationr   Úconfiguration_mllamar   r   r   Ú!torch.nn.attention.flex_attentionr   Úintegrations.flex_attentionr    Ú
get_loggerrb   r«   re   r  rr  r<   r#   rI   ÚModulerK   ri   rx   r…   r§   r¸   r²   rÉ   ré   rø   r  r(  r2  r  r9  rC  rX  rP  rF  rN  rV  r^  rz  ÚMLLAMA_START_DOCSTRINGrã  r   r)  r…  rå  r  r+  Ú__all__rÓ   r=   r;   ú<module>rY     sK  ðñ ã ß /Ó /ã ß Ð Û Ý å Ý !ß ;Ñ ;Ý )Ý >ß `Ñ `ß K÷õ õ 1ß TÑ Tñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€ð?ØŸ,™,ð?àð?ð ð?ð ˆ5�<‰<˜Ÿ™Ð%Ñ&ó	?ð8Ø—|‘|ðàðð ðð �;‰;ð	ð
 ‡\�\óô6¨B¯I©Iô ô."¨¯©ô "ôL�b—i‘iô ô0$˜BŸI™Iô 0$ôf*Ð 5ô *ðZ -BÐKdÑ"eÐ ô.˜rŸy™yô .ôbT
˜"Ÿ)™)ô T
ôpJ˜Ÿ	™	ô Jô(S9˜rŸy™yô S9ôl[1Ð#;ô [1ò~(óð8	U˜UŸ\™\ð 	U°#ð 	U¸%¿,¹,ó 	UôE9˜bŸi™iô E9ôPO1Ð"9ô O1ðd 1IÐRnÑ&oÐ #Ø*AÐKfÑ gÐ ô�B—I‘Iô ô$P b§i¡iô Pôf9 u§x¡x§¡ô 9ôx<˜BŸI™Iô <ô:b˜Oô bðJÐ ð""Ð ðDR Ð ðjgÐ ñT ØHØóôQ
Ð-ó Q
ó	ðQ
ñh ØcØóôv
Ð+ó v
ó	ðv
ñr ØEØóôB
Ð-¨ó B
ó	ðB
ñJ ØSØóôVÐ%:¸Oó Vó	ðVòr�r=   