
    PhB                     $   d dl mZmZmZ d dlZd dlZd dlmZ d dlmc m	Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZ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*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1  G d dejd                        Z3 G d dejd                        Z4 G d dejd                        Z5 G d de      Z6dejn                  de8dejn                  fdZ9	 dVdejd                  d ejn                  d!ejn                  d"ejn                  d#eejn                     d$e:d%e:d&e"e$   fd'Z;d( Z<dWd)Z= G d* d+ejd                        Z> G d, d-ejd                        Z?e% G d. d/e              Z@ G d0 d1e@      ZA G d2 d3e@      ZB G d4 d5e@      ZCd6ejn                  d7ee8   dejn                  fd8ZD G d9 d:e@      ZEdXd;e8fd<ZF	 dYd=ejn                  d>e8d;e8d?e8fd@ZGdAejn                  dBej                  dCe8dDeIdEe8dejn                  fdFZJdGdej                  fdHejn                  dIe8dJe8dKeLdLe8dMej                  deNejn                  ejn                  f   fdNZO G dO dPe@      ZP e%dQR       G dS dTe@e             ZQg dUZRy)Z    )CallableOptionalUnionN   )ACT2FN)CacheDynamicCache)GenerationMixin)create_causal_mask)FlashAttentionKwargs)GradientCheckpointingLayer)BaseModelOutputWithPastCausalLMOutputWithPast)ROPE_INIT_FUNCTIONSdynamic_rope_update)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tuple)deprecate_kwarg)OutputRecordercheck_model_inputs   )	BltConfigBltGlobalTransformerConfigBltLocalDecoderConfigBltLocalEncoderConfigBltPatcherConfigc                   $     e Zd Z fdZd Z xZS )BltMLPc                    t         |           || _        |j                  | _        |j                  | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _	        t        |j                     | _        y NFbias)super__init__confighidden_sizeintermediate_sizennLinear	gate_projup_proj	down_projr   
hidden_actact_fnselfr)   	__class__s     ^/var/www/html/saasai/venv/lib/python3.12/site-packages/transformers/models/blt/modeling_blt.pyr(   zBltMLP.__init__4   s    !--!'!9!94#3#3T5K5KRWXyy!1!143I3IPUV4#9#94;K;KRWXV../    c                     | j                  | j                  | j                  |            | j                  |      z        }|S N)r0   r2   r.   r/   )r4   xr0   s      r6   forwardzBltMLP.forward?   s6    NN4;;t~~a/@#ADLLQRO#ST	r7   )__name__
__module____qualname__r(   r;   __classcell__r5   s   @r6   r"   r"   3   s    	0r7   r"   c                   ,     e Zd Zd fd	Zd Zd Z xZS )
BltRMSNormc                     t         |           t        j                  t	        j
                  |            | _        || _        y)z9
        BltRMSNorm is equivalent to T5LayerNorm
        N)r'   r(   r,   	Parametertorchonesweightvariance_epsilon)r4   r*   epsr5   s      r6   r(   zBltRMSNorm.__init__E   s1     	ll5::k#:; #r7   c                 "   |j                   }|j                  t        j                        }|j	                  d      j                  dd      }|t        j                  || j                  z         z  }| j                  |j                  |      z  S )N   T)keepdim)	dtypetorE   float32powmeanrsqrtrH   rG   )r4   hidden_statesinput_dtypevariances       r6   r;   zBltRMSNorm.forwardM   sy    #))%((7 $$Q',,R,>%Ht?T?T4T(UU{{]--k:::r7   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tuplerG   shaperH   r4   s    r6   
extra_reprzBltRMSNorm.extra_reprT   s*    ))*+6$2G2G1HIIr7   )gư>)r<   r=   r>   r(   r;   r[   r?   r@   s   @r6   rB   rB   D   s    $;Jr7   rB   c                   ~     e Zd ZU ej                  ed<   ddef fdZ ej                         e	d               Z
 xZS )BltRotaryEmbeddinginv_freqr)   c                    t         |           t        |d      rUt        |j                  t
              r;|j                  j                  d|j                  j                  d            | _        nd| _        |j                  | _	        |j                  | _
        || _        t        | j                     | _        | j                  | j                  |      \  }| _        | j                  d|d       | j                   | _        y )Nrope_scaling	rope_typetypedefaultr^   F)
persistent)r'   r(   hasattr
isinstancer`   dictgetra   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenr)   r   rope_init_fnattention_scalingregister_bufferr^   original_inv_freq)r4   r)   devicer^   r5   s       r6   r(   zBltRotaryEmbedding.__init__[   s    6>*z&:M:Mt/T#0044[&BUBUBYBYZ`BabDN&DN"("@"@$*$B$B!/?+/+<+<T[[&+Q($(ZeD!%r7   c                 .   | 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                  |dd	      }|j                         | j                  z  }|j                         | j                  z  }	d d d        j                  |j                   
      	j                  |j                   
      fS # 1 sw Y   AxY w)Nr   rL   r   mpscpuF)device_typeenabledrK   dim)rN   )r^   floatexpandrY   rf   rp   rb   strrE   autocast	transposerepeat_interleavecosrm   sinrO   rN   )
r4   r:   position_idsinv_freq_expandedposition_ids_expandedrt   freqsembr~   r   s
             r6   r;   zBltRotaryEmbedding.forwardl   s?    !MM$4-8>>@GGHZHZ[\H]_acde ,QaZ 8 > > @'1!((--'E!((--[`J`ahhmmfk^^UC&,,.1F1L1L1NNYYZ[]^_E))%;C'')d444C'')d444C	 D vvAGGv$cff177f&;;; DCs   BFFr9   )r<   r=   r>   rE   Tensor__annotations__r   r(   no_gradr   r;   r?   r@   s   @r6   r]   r]   X   s=    ll/y /" U]]_<  <r7   r]   c                       e Zd Zdef fdZ eddd      	 	 	 	 	 	 	 	 	 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j                     dee	ej                  ej                  f      dee   de	ej                  ee	ej                  ej                  f      f   fd       Z xZS )BltTransformerLayer	layer_idxc                 .   t         |           |j                  | _        t        ||      | _        t        |      | _        t        |j                  |j                        | _	        t        |j                  |j                        | _
        || _        y )N)r)   r   rI   )r'   r(   r*   BltSelfAttention	self_attnr"   mlprB   rms_norm_epsinput_layernormpost_attention_layernormr   r4   r)   r   r5   s      r6   r(   zBltTransformerLayer.__init__~   sr    !--)9M&>)&*<*<&BUBUV(263E3E6K^K^(_%"r7   past_key_valuepast_key_values4.58new_nameversionrT   cross_attention_statescross_attention_maskattention_maskfull_text_row_masked_out_maskr   	use_cachecache_positionposition_embeddingskwargsreturnc                     |}| j                  |      } | j                  d||||||	|
d|\  }}||z   }|}| j                  |      }| j                  |      }||z   }|S )aG  
        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.

            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_values (`Cache`, *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
        )rT   r   r   r   r   r   r    )r   r   r   r   )r4   rT   r   r   r   r   r   r   r   r   r   r   residualself_attn_weightss                 r6   r;   zBltTransformerLayer.forward   s    F !,,]; ,:4>> 	,
')%+) 3	,
 	,
(( !=0 !55mD/ =0r7   )	NNNNNNFNN)r<   r=   r>   intr(   r   rE   r   r   rX   
LongTensorr   boolr   r   FloatTensorr;   r?   r@   s   @r6   r   r   }   s^   	## 	# %0A6R :>7;15UY37+/$)59KO9||9 !) 69 'u||4	9
 !.9 (0ellELL6P0Q'R9 u//09 "%9 D>9 !!1!129 &eELL%,,,F&GH9 -.9 
u  (51B1BEDUDU1U+V"WW	X9 S9r7   r   rT   n_repr   c                     | 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)rY   ry   reshape)rT   r   batchnum_key_value_headsslenhead_dims         r6   	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr7   modulequerykeyvaluer   scalingdropoutr   c                 T   t        || j                        }t        || j                        }	t        j                  ||j	                  dd            |z  }
|#|d d d d d d d |j
                  d   f   }|
|z   }
t        j                  j                  |
dt        j                        j                  |j                        }
t        j                  j                  |
|| j                        }
t        j                  |
|	      }|j	                  dd      j                         }||
fS )NrK   r   rL   )rw   rN   ptrainingr   )r   num_key_value_groupsrE   matmulr|   rY   r,   
functionalsoftmaxrP   rO   rN   r   r   
contiguous)r   r   r   r   r   r   r   r   
key_statesvalue_statesattn_weightscausal_maskattn_outputs                r6   eager_attention_forwardr      s    3 ; ;<JUF$?$?@L<<z';';Aq'ABWLL!$Q1.D
0@0@0D.D%DE#k1==((2U]](SVVW\WbWbcL==((6??([L,,|\:K''1-88:K$$r7   c                     | dd d df   }| ddd df   }t        j                  | |gd      j                  d      }|S )N.rK   r   rL   rv   r   )rE   stackflatten)r:   x1x2rot_xs       r6   rotate_halfr      sL    	
3!8B	
319BKK"b	r*2226ELr7   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.
    )	unsqueezer   )qkr~   r   r   unsqueeze_dimq_embedk_embeds           r6   apply_rotary_pos_embr      sY    ( --
&C
--
&C3w;q>C/0G3w;q>C/0GGr7   c            
            e Zd Zdedef fdZ eddd      	 	 	 ddej                  d	ej                  d
ej                  de	fd       Z
 xZS )r   r)   r   c                    t         |           || _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  | j                  z  | _        | j                  | j                  z  | _	        | j                  d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      | _        d| _        y )N      Fr%   T)r'   r(   r)   num_attention_heads	num_headsr   r*   r   r   r   r   
rope_thetar   r,   r-   q_projk_projv_projo_proj	is_causalr   s      r6   r(   zBltSelfAttention.__init__  sE   33~~!--#)#=#= **dnn<$(NNd6N6N$N!}}d* ++"ii 0 0$..4==2PW\]ii 0 0$2J2JT]]2Zafgii 0 0$2J2JT]]2Zafgii >@P@PW\]r7   r   r   r   r   rT   r   r   r   c                    |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                  j                  dk7  rt        | j                  j                     } || ||||f| j                   sdn| j"                  | j$                  d|\  }}|j'                  ||	d      j)                         }| j+                  |      }||fS )Nr   rK   )r   r~   r   eager        r   r   rL   )sizer   r   r   viewr   r   r|   r   r   updater   r   r)   _attn_implementationr   r   r   r   r   r   r   )r4   rT   r   r   r   r   r   r   bszq_len_query_statesr   r   r~   r   cache_kwargsattention_interfacer   r   s                       r6   r;   zBltSelfAttention.forward#  s    &**,UA{{=1[[/
{{=1#((eT^^T]]S]]^_abc__S%1I1I4==Yccdeghi
#((eT5M5Mt}}]gghiklm&S#7jRUWZ#[ j&#&snUL'6'='=j,X\XfXfht'u$J(?;;++w6"9$++:Z:Z"[$7	%
  $}}C$,,LL	%
 	%
!\ "))#ub9DDFkk+.L((r7   )FNN)r<   r=   r>   r   r   r(   r   rE   r   r   r;   r?   r@   s   @r6   r   r     ss    y S & %0A6R  /)||/) /) #\\	/)
 /) S/)r7   r   c                   J    e Zd ZdZddededee   f fdZ eddd	      	 	 	 	 dd
e	j                  dee	j                     dee   dee	j                     dee	j                     dee   dee	j                  ee	j                     eee	j                        f   fd       Z xZS )BltCrossAttentionz<Cross-attention module for Blt, following transformers styler)   r   r*   c                 $   t         |           || _        | j                  j                  | _        | j                  j
                  | _        |j                  | _        |j                  | _        |j                  | j                  z  | _        || _	        | j                  | j
                  z  | _
        | j                  d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&                        | _        d| _        y )Nr   Fr%   r   )r'   r(   r)   r   r   r   r   r*   r   r   r   r   r,   r-   r   r   r   r   rB   r   q_normk_normr   )r4   r)   r   r*   r5   s       r6   r(   zBltCrossAttention.__init__Y  st   88#';;#B#B ~~!--**dnn<"$(NNd6N6N$N!}}d*ii 0 0$..4==2PW\]ii 0 0$2J2JT]]2Zafgii 0 0$2J2JT]]2Zafgii >@P@PW\] !1!1v7J7JK !1!1v7J7JKr7   r   r   r   r   rT   r   r   r   r   r   c                    |j                         \  }}}	| 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                  d|i      \  }}nZ|d   dk7  rG|j                  | j                     j                  |j                  | j                     j                  }}nt!        d      t"        }| j$                  j&                  dk7  rt(        | j$                  j&                     } || |
|||f| j*                  sdn| j,                  | j.                  d	|\  }}|j1                  ||d      j3                         }| j5                  |      }||z   }||fS )
z#Input shape: Batch x Time x Channelr   rK   rL   r   r   z^Cross attention layer can't find neither `cross_attn_states` nor cached values for key/values!r   r   r   )r   r   r   r   r   r   r|   r   r   r   r   r   r   layerskeysvalues
ValueErrorr   r)   r   r   r   r   r   r   r   r   )r4   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                   r6   r;   zBltCrossAttention.forwardm  s2    &**,UA{{=1{{<0#((eT^^T]]S]]^_abc!-%)[[1G%H"%;<J;;'=>L#b$2J2JDMMZddefhijJ',,S"d6N6NPTP]P]^hhijlmnL*+:+A+Adnn?OQ_>`,(
L A!#&&t~~6;;&&t~~6== %J
 p  )@;;++w6"9$++:Z:Z"[$7	%
  $}}C$,,LL	%
 	%
!\ "))#ub9DDFkk+.!M1L((r7   r9   NNNN)r<   r=   r>   __doc__r   r   r   r(   r   rE   r   r   r   r   r   rX   r;   r?   r@   s   @r6   r   r   V  s    Fy S xPS} ( %0A6R :>+/15594)||4) !) 64) "%	4)
 !.4) !!1!124) +,4) 
u||Xell3XeELL>Q5RR	S4) S4)r7   r   c                   h    e Zd ZU eed<   dZdZdgZdZdZ	dZ
dZdZ eedd       eed	d      d
Zy)BltPreTrainedModelr)    Tr   Fr   local_decoderindex
layer_namer   )rT   
attentionsN)r<   r=   r>   r   r   base_model_prefixsupports_gradient_checkpointing_no_split_modules_can_compile_fullgraph_supports_sdpa_supports_flash_attn_supports_flex_attn_supports_attention_backendr   r   r   _can_record_outputsr   r7   r6   r   r     s]    &*#./"N "''(;1Q`a$%5Q?[r7   r   c                   |    e Zd ZU eed<   d eedd      iZdef fdZ	 	 	 	 	 	 	 	 	 	 dde	e
j                     de	e
j                     d	e	e
j                     d
e	e
j                     de	e
j                     de	e   de	e
j                     de	e
j                     de	e   de	e
j                     dee   fdZd Z xZS )BltLocalEncoderr)   encoder_attentionsr   local_encoderr  c           	         t         |   |       d| _        || _        t	        j
                  t        |j                        D cg c]  }t        ||       c}      | _	        t        |      | _        t	        j                  |j                  |j                  |j                  z  d      | _        t	        j                   |j"                  |j                        | _        t	        j
                         | _        |j(                  r|j                  nd}t        |      D ]3  }| j&                  j+                  t-        |||j                               5 | j/                          y c c}w )NFr)   in_featuresout_featuresr&   r   r)   r   r*   )r'   r(   gradient_checkpointingr)   r,   
ModuleListrangenum_hidden_layersr   r   r]   
rotary_embr-   r*   cross_attn_kpatch_embedding_projection	Embedding
vocab_sizeembed_tokenscross_attn_layerscross_attn_all_layersappendr   	post_initr4   r)   r   layers_to_addr5   s       r6   r(   zBltLocalEncoder.__init__  s"    &+#mmEJ6KcKcEdeEd	 3Ede
 -F;*,))**++f.A.AA+
'
 LL):):F<N<NO!#4:4P4P00VW}-I""))!9RXRdRde .
 	! fs   E'	input_idsinputs_embedspatch_embedsr   r   r   r   encoder_attention_masknum_patches	patch_idsr   c           	         || j                  |      }|j                  d   }t        j                  || j                  j                  | j
                        }|Mt        j                  |j                  d   |j                        j                  d      j                  |d      }| j                  ||      }t        j                  || j                  j                  | j
                        }t        | j                        D ]  \  }} ||f||||d|}|t        | j                        dz
  k(  s| j                  j                  sF| j!                  ||	|
      }| j#                  |      }|j%                  ||j                  d   | j                  j&                  z  | j                  j(                        }| j                  j                  r|nd} | j*                  |   d|||d|\  }}||z   } |}||fS )	Nr   r   r   rp   rL   r   r   r   r   rT   r   r   r   )r!  rY   Fr   r)   r   rE   arangerp   r   ry   r  	enumerater   lenr#  patch_reducer  r   r  r*   r"  )r4   r(  r)  r*  r   r   r   r   r+  r,  r-  r   
batch_sizerT   r   idxlayerr   cross_attention_outputr   encoder_cross_statess                        r6   r;   zBltLocalEncoder.forward  s      --i8M"((+
		-4;;3F3FQUQ^Q^_]003M<P<PQ[[\]^eefprtu  #oom\J		-4;;3F3FQUQ^Q^_#DKK0JC!$7- /- M c$++&**dkk.O.O#00YW#>>|L+33 2 21 58P8P PRVR]R]RiRi  $(;;#D#DC!	,MD,B,B9,M -".+8#9- 	-)&  ,.DD- 1.  ,222r7   c                 F   |j                   d   }|j                   d   }|j                  d      j                  dd|j                   d         }t        j                  |||f|j
                  |j                        }|j                  |d|dd      }|ddd|ddf   }|S )	a  
        Reduce variable length patches to single embedding per patch
        Note: this works with variable number of patches for different sequences in the batch
        It handles variable length patches by assuming that patch_lengths will be 0 for any
        extra patches on the *right*. Since there can be a variable number of patches
        this function also return the number of patches for each sequence in the batch.
        Any embeddings on the right that are not allocated to a patch
        (i.e. if the sum(patch_lengths[i]) < seq_len for any i)
        will be sent to a dummy patch, which is trimmed before returning.
        r   rL   rN   rp   r   amaxF)srcrw   r  reduceinclude_selfN)rY   r   ry   rE   zerosrN   rp   scatter_reduce)r4   rT   max_num_patchesr-  r7  embedding_dimreduced_embeddingss          r6   r6  zBltLocalEncoder.patch_reduce	  s     #((+
%++B/''+222r=;N;Nr;RS	"[[-8@S@S\i\p\p
 0>> ? 
 03CO3CQ0FG!!r7   
NNNNNNNNNN)r<   r=   r>   r   r   r   r   r  r(   r   rE   r   r   r   r   r   r   r;   r6  r?   r@   s   @r6   r  r    s!   !!n-=QSbc4 2 1504/31537+/599=%),043E,,-43  -43 u||,	43
 !.43 u//043 "%43 !!1!1243 !) 643 c]43 ELL)43 +,43l"r7   r  c                   :    e Zd ZU eed<   def fdZe	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     deej                     dee   d	eej                     d
eej                     dee   fd       Z xZS )BltLocalDecoderr)   c           	         t         |   |       d| _        || _        d| _        t        j                  t        |j                        D cg c]  }t        ||       c}      | _
        t        |      | _        t        j                  |j                  |j                  |j                   z  d      | _        t%        |j                  |j&                        | _        t        j                         | _        |j,                  r|j                  nd}t        |      D ]3  }| j*                  j/                  t1        |||j                               5 | j3                          y c c}w )NFTr  r  r   r   r  )r'   r(   r  r)   cross_attn_decoderr,   r  r  r  r   r   r]   r  r-   hidden_size_globalr*   r  r  rB   r   normr"  r#  r$  r   r%  r&  s       r6   r(   zBltLocalDecoder.__init__,  s%    &+#"&mmEJ6KcKcEdeEd	 3Ede
 -F;*,))11++f.A.AA+
'
 v11v7J7JK	!#4:4P4P00VW}-I""))!9RXRdRde .
 	! fs   E%r(  r)  r*  r   r   r   r   r+  r   c	           	      $   |j                   d   }
|}| j                  |      }|j                  |
|j                   d   | j                  j                  z  | j                  j
                        }|| j                  s||z   }|Mt        j                  |j                   d   |j                        j                  d      j                  |
d      }| j                  ||      }t        j                  || j                  j                  | j                        }t!        | j"                        D ]O  \  }}|dk(  s| j                  j$                  r! | j&                  |   d|||d|	\  }}||z   } ||f||||d|	}Q | j)                  |      }|S )	Nr   r   r/  rL   r   r1  r0  r   )rY   r  r   r)   r  r*   rK  rE   r3  rp   r   ry   r  r2  r   r   r4  r   r#  r"  rM  )r4   r(  r)  r*  r   r   r   r   r+  r   r7  rT   r   ir9  r:  r   logitss                     r6   r;   zBltLocalDecoder.forwardD  s    #((+
%66|D#++**1-0H0HH$++JaJa
 #D,C,C)L8M]003M<P<PQ[[\]^eefprtu  #oom\J		-4;;3F3FQUQ^Q^_!$++.HAuAv::,ED,B,B1,E -"/+7#9- 	-)& !.0F F!$7- /- M /" =)r7   NNNNNNNN)r<   r=   r>   r   r   r(   r   r   rE   r   r   r   r   r   r;   r?   r@   s   @r6   rI  rI  )  s    !!4 0  1504/31537+/599=0E,,-0  -0 u||,	0
 !.0 u//00 "%0 !!1!120 !) 60 +,0 0r7   rI  c                        e Zd ZU eed<   d eedd      iZdef fdZ	 	 	 	 dde	j                  dee	j                     d	ee	j                     d
ee   dee	j                     dee   fdZ xZS )BltGlobalTransformerr)   global_attentionsr   global_transformerr  c                    t         |   |       || _        t        j                         | _        t        |j                        D ]'  }| j
                  j                  t        ||             ) t        |      | _        t        |dd       2t        j                  |j                  |j                  d      | _        nt        j"                         | _        | j%                          y )Nr  encoder_cross_output_sizeFr%   )r'   r(   r)   r,   r  r   r  r  r$  r   r]   r  getattrr-   rW  r*   token_embedding_projectionIdentityr%  r   s      r6   r(   zBltGlobalTransformer.__init__~  s     mmov778IKK269EF 9,F; 66=I.0ii00&2D2D5/D+ /1kkmD+r7   input_embedsr   r   r   r   r   c           	         |j                   \  }}}	| j                  |      }
t        j                  |
| j                  j                  | j
                        }
|Mt        j                  |j                   d   |j                        j                  d      j                  |d      }| j                  |
|      }t        | j                        D ]  \  }} ||
f||||d|}
 |
S )Nr   r   r/  r   rL   r0  )rY   rY  r2  r   r)   r   rE   r3  rp   r   ry   r  r4  r   )r4   r[  r   r   r   r   r   r7  seq_lenr   rT   r   rO  r9  s                 r6   r;   zBltGlobalTransformer.forward  s     ".!3!3
GQ77E		-4;;3F3FQUQ^Q^_\//2<;N;NOYYZ[\ccdnprs  #oom\J!$++.HAu!$7- /- M / r7   r   )r<   r=   r>   r   r   r   r   r  r(   rE   r   r   r   r   r   r   r;   r?   r@   s   @r6   rS  rS  x  s    &&^,<ARfg9 * 2637+/59ll !. u//0	
 "% !!1!12 +,r7   rS  patch_lengthsmax_patch_lengthc                 6   || S | j                  d      }g }| D ]j  }g }||dkD     D ]J  }|j                         }t        ||      \  }}|j                  |g|z         |s:|j	                  |       L |j	                  |       l t        d |D              }	t        j                  ||	f| j                  | j                        }
t        |      D ]D  \  }}|s	t        j                  || j                  | j                        |
|dt        |      f<   F |
dk7  j                  d      j                         |
j                  d   k  rM|
dk7  j                  d      j!                         j                         j                         dz   }|
ddd|f   }
|
S )a  
    Splits patch lengths into smaller segments if they exceed `max_patch_length`.
    Pads the result to uniform length across the batch.

    Args:
        patch_lengths (torch.Tensor): [batch_size, num_patches] tensor of patch lengths.
        max_patch_length (int, optional): Maximum allowed length per patch.

    Returns:
        torch.Tensor: [batch_size, max_len] tensor of split and padded patch lengths.
    Nr   c              3   2   K   | ]  }t        |        y wr9   )r5  ).0splitss     r6   	<genexpr>z(process_patch_lengths.<locals>.<genexpr>  s     6I&#f+Is   r=  rv   r   )r   itemdivmodextendr$  maxrE   rB  rN   rp   r4  tensorr5  anysumrY   nonzero)r^  r_  r7  	processedseqrc  lengthfull_chunks	remaindermax_lenpaddedrO  last_nonzeros                r6   process_patch_lengthsru    s    ##A&JI#'lF[[]F%+F4D%E"KMM+,{:;i( # 	   6I66G[[*g.m6I6IR_RfRfgFy)	6',||F-BUBU^k^r^r'sF1mFm#$ *
 	!Q##%Q7!((Q(/779==?DDFJ=L=()Mr7   c                   6    e Zd ZU eed<   def fdZ	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     dee
   deej                     dee   d	eej                     d
ee   dee   dee   dee   fdZe	 	 dd       Z xZS )
BltPatcherr)   c                    t         |   |       t        | j                        | _        t        j                         | _        t        | j                  j                        D ]1  }| j                  j                  t        | j                  |             3 t        j                  | j                  j                  | j                  j                        | _        t!        | j                  j                  | j                  j"                        | _        t        j&                  | j                  j                  | j                  j                  d      | _        y )Nr  r   Fr%   )r'   r(   r]   r)   r  r,   r  r   r  r  r$  r   r  r   r*   r!  rB   r   rM  r-   lm_headr   s      r6   r(   zBltPatcher.__init__  s     ,DKK@mmot{{<<=IKK24;;	JK >LL)?)?AXAXYt{{66DKK<T<TU	yyKK##KK""
r7   r(  r   r   r   r)  r   r   
patch_size	thresholdr_  r   c                 &   |d u |d uz  rt        d      || j                  |      }|r|
t               }|F||j                         nd}t	        j
                  |||j                  d   z   |j                        }||j                  d      }t        | j                  |||||      }|}| j                  ||      }| j                  D ]  } ||||      } | j                  | j                  |            }t        j                  j!                  |      j#                         }|j                  d d \  }}|| j%                  ||||		      }n.t	        j&                  ||f|j(                  |j                  
      }t+        ||
      }|||fS )N:You must specify exactly one of input_ids or inputs_embedsr   r   r/  r)   r[  r   r   r   r   )r   r   )rP  rK   )	entropiessequence_lengthrz  r{  r=  )r   r!  r	   get_seq_lengthrE   r3  rY   rp   r   r   r)   r  r   ry  rM  distributionsCategoricalentropypatch_lengths_from_entropiesrF   rN   ru  )r4   r(  r   r   r   r)  r   r   rz  r{  r_  r   past_seen_tokensr   rT   r   r9  rP  prediction_entropiesr7  r  r^  s                         r6   r;   zBltPatcher.forward  s    -t";<YZZ  --i8M0*nO!CRC^==?de"\\ "2]5H5H5K"KTaThThN )33A6L(;;&))+%
 &"oom\J[[E!-EXituM ! dii67$22>>f>MUUW&3&9&9"1&=#
O! ==. /%#	 > M "JJ_-]5H5HQ^QeQeM .m=MN#]F::r7   c                    | j                   d   }t        j                  ddgt        j                  | j                        j                  d      j                  |d      }|j                   d   }| ddddf   } | |kD  }|j                   d   }t        j                  || j                        j                  d      j                  |d      }	t        j                  |	|      }
t        j                  |	|
gd      }t        j                  || gd      }||   j                  ||      }|j                  d      j                         }|ddd|f   }t        j                  |||z   fd      }t        j                  |ddddf   |dz
        }t        j                  |ddddf   dz
  |fd      }||z
  dz   }|S )z
        Computes patch lengths from token entropies.

        Depending on whether a threshold is provided, the function uses either:
        - Thresholding the entropy values (when `threshold` is set).
        r   r   r=  Nr/  rL   rv   )rY   rE   ri  longrp   r   repeatr3  ry   	full_likecatr   rk  rh  )r  r  rz  r{  r7  init_tokensoffset
patch_maskr]  token_indicessentinelpadded_indicespadded_maskpatch_startsmax_valid_patchespatch_start_ids
last_token
patch_endsr^  s                      r6   r  z'BltPatcher.patch_lengths_from_entropies+  s    __Q'
 LL!Quzz):J:JKUUVWX__`jlmn 	 ""1% ae$	 *
""1% WY5E5EFPPQRSZZ[egij??=':M8#<!D iij[ 9qA &k2:::wO&NNqN1557#A'9(9'9$9:  ))[,2G$HaP ___QU%;_q=PQ
YY12 6 :JGQO
"_4q8r7   rG  )NN)r<   r=   r>   r    r   r(   r   rE   r   r   r   r   r   r   rx   r   r   r;   staticmethodr  r?   r@   s   @r6   rw  rw    s   
/ 
  151537+/59$(59$(%)*.?;E,,-?; !.?; u//0	?;
 "%?;   1 12?; D>?; !!1!12?; SM?; E??; #3-?; +,?;B  	3 3r7   rw  primec                     t        j                  |t         j                  | j                        }t        j                  | j
                  d   | j                        }||z  }t        j                  | |z  d      S )a  
    A polynomial rolling hash algorithm that converts sequences
    of tokens into hash values. The hash is computed as:
        hash = (token_0 * prime^0 + token_1 * prime^1 + ... + token_n * prime^n)

    The rolling hash allows the model to efficiently
    identify and encode recurring byte-level patterns in the input text.

    Args:
        token_tensor (torch.Tensor): [batch_size, seq_len, group_size] containing token IDs to hash
        prime (int): Prime number used as the base for the polynomial hash.

    Returns:
        torch.Tensor: Hash values of shape [batch_size, seq_len] where each value
                     represents the hash of the corresponding token group

    Example:
        >>> tokens = torch.tensor([[1, 2, 3], [4, 5, 6]])
        >>> hashes = rolling_polynomial_hash(tokens, prime=31)
        >>> # hash[0] = 1*31^0 + 2*31^1 + 3*31^2
        >>> # hash[1] = 4*31^0 + 5*31^1 + 6*31^2
    r=  rL   r/  rv   )rE   ri  int64rp   r3  rY   rk  )token_tensorr  prime_tensorpowersprime_powerss        r6   rolling_polynomial_hashr  b  sa    . <<U[[ATATUL\\,,,R09L9LMF'L99\L0b99r7   	token_ids
group_sizemax_hashc                 Z   t        j                         5  | j                  \  }}t        j                  ||dz
  t         j                  | j
                        }t        j                  || gd      }|j                  d|d      }t        ||      }	|	|z  }
ddd       |
S # 1 sw Y   
S xY w)z1Hash token groups and map to range [0, max_hash].r   r=  rv   N)	rE   r   rY   rB  r  rp   r  unfoldr  )r  r  r  r  r7  r]  paddingpadded_tokenswindowshasheshash_valuess              r6   byte_group_hash_functionr    s     
'oo
G++j*q.T]TdTde		7I"6A>  &&q*a8(%8x' 
  
 s   BB  B*local_encoder_tokensencoder_hash_tok_embedding$encoder_hash_byte_group_nb_functionsencoder_hash_byte_group_sizeencoder_hash_byte_group_vocabc                     g d}|j                  |       }d}t        |      D ]@  }	||	t        |      z     }
|D ](  }t        | ||
|      }|||z  z   }| ||      z  }|dz  }* B |S )z=Compute token embeddings enhanced with hash-based embeddings.)ʚ;l   21A ioYl   vt l   . l   }g l   Au l   0 l   T l   AK l   | r   r   )r!  r  r5  r  )r  r  r  r  r  r  primes
embeddingsembedding_idxfunc_nbr  r  hash_idsoffset_hash_idss                 r6   compute_hash_embeddingsr    s    F ++,@AJM=>wV,-6J/0DjRWYvwH&9V)VVO4_EEJQM 7 ? r7   Fr-  r,  r  patches_as_queriesr  rN   c                 z   | j                   \  }}| j                  }|rp||z  }	|}
t        j                  ||      j	                  d      j	                  d      j                  |||      }| j	                  d      j                  |||      }no|}	||z  }
| j	                  d      j                  |||      }t        j                  ||      j	                  d      j	                  d      j                  |||      }||k(  }|rdnd}|j                  ||      }||	|
f}|j                   |k7  rt        d|j                    d|       |j	                  d      }d|j                  |      z
  }|j                  |j                  t        j                        t        j                  |      j                        }|S )	aR  
    Prepare cross-attention mask for patch-based attention, following mllama's robust approach.

    This function creates masks that control which patches can attend to which other patches,
    with support for query/key role swapping and cross-attention multipliers.

    Args:
        patch_ids (torch.Tensor): Tensor of shape [batch_size, seq_len] containing patch ids.
        num_patches (int): Total number of patches.
        sequence_length (int): Length of the sequence.
        patches_as_queries (bool): If True, patches are used as queries, otherwise as keys.
        cross_attn_k (int): Cross-attention multiplier for repeating patches.
        dtype (torch.dtype): Data type for the output mask.

    Returns:
        Tuple[torch.Tensor, torch.Tensor]:
            - cross_attention_mask: 4D tensor [batch_size, 1, q_len, kv_len]
    r/  r   rL   r   rv   zCross attention mask shape z doesn't match expected g      ?)rY   rp   rE   r3  r   ry   r}   r   rO   masked_fillr   finfomin)r-  r,  r  r  r  rN   r7  r]  rp   r   kv_lenq_patch_idskv_patch_idsr   
repeat_dimexpected_shapeinverted_cross_attn_masks                    r6   #_prepare_patch_cross_attention_maskr    s   4 $//JF l*  LLV4Yq\Yr]VJW5	 	 !**1-44ZgV|+))"-44Z+VLLV4>>qAKKANUUV`bikvw 	 ',6 )bJ/AA,T^A_ !%0N!!^3)*>*D*D)EE]^l]mn
 	

 099!<  #%9%<%<U%CC3?? ##EJJ/U1C1G1G  r7   c                   f    e Zd Zdef fdZe	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     dee
   deej                     d	ee   d
eej                     dee   defd       Zd Zd Zdej                  dedej                  fdZ xZS )BltModelr)   c                    t         |   |       d| _        || _        t	        |j
                        | _        t        |j                        | _	        t        |j                        | _        |j                  t        |j                        z  }|j                   |z  }t#        j$                  ||j
                  j&                        | _        | j                  j*                  r[t-        |j.                        | _        | j0                  j3                          | j0                  j5                         D ]	  }d|_         nd | _        | j9                          y )NF)r'   r(   r  r)   r  encoder_configr  rS  global_configrU  rI  decoder_configr   r  r5  r  r  r,   r  r*   r  patch_in_forwardrw  patcher_configpatchereval
parametersrequires_gradr%  )r4   r)   num_embeddingstotal_vocab_sizeparamr5   s        r6   r(   zBltModel.__init__  s    &+#,V-B-BC"6v7K7K"L,V-B-BCDDs6KnKnGoo!??.P*,,,7GI^I^IjIj*k';;''%f&;&;<DLLL002&+# 3  DLr7   r(  r^  r   r   r   r)  r   r   r   r   c	                 b   |d u |d uz  rt        d      ||}
|j                  \  }}}no|j                  \  }}t        || j                  | j                  | j
                  j                  | j
                  j                  | j
                  j                        }
|| j
                  j                  dk(  r| j                  |t        d      | j                  || j
                  j                  | j
                  j                  | j
                  j                  | j
                  j                  |j                        \  }}}no||j                  n|j                  }||j                   n|j                   }t#        t%        j&                  ||dz   f||      | j
                  j                        }| j)                  ||      }|F||j+                         nd}t%        j,                  |||
j                  d   z   |
j                        }||j/                  d      }t1        | j
                  |
||||	      }t3        ||j                  d   |d
| j
                  j4                  |
j                         } | j                  d||
||||j                  d   |d|	\  }}|j7                  ||j                  d   d      }t%        j,                  d|j                  d   |j                        }|j/                  d      }t1        | j
                  |d |d d 	      } | j8                  d|||d|	}| j)                  |d d dd f   |      }t3        ||j                  d   |d| j
                  j4                  |
j                         } | j:                  d||||||||d|	}t=        ||      S )Nr}  r  z0input_ids is required for entropy-based patching)rz  r{  r_  patching_batch_sizerp   r   r=  r   r/  r~  T)r-  r,  r  r  r  rN   )r(  r)  r   r   r+  r,  r-  rL   )r[  r   r   F)r(  r)  r*  r   r   r   r   r+  )last_hidden_stater   r   )r   rY   r  r  r  r)   r  r  r  patching_moder  rz  patching_thresholdr_  r  rp   rN   ru  rE   rF   _patch_ids_from_lengthsr  r3  r   r   r  r  r   rU  r   r   )r4   r(  r^  r   r   r   r)  r   r   r   encoder_embedsr7  r  r   rp   rN   r-  r  r   cross_attn_mask_encencoder_hidden_statesr;  global_cache_positionglobal_position_idsglobal_causal_maskglobal_hidden_statesdecoder_patch_idscross_attn_mask_decoutputs                                r6   r;   zBltModel.forward  s    -t";<YZZ $*N-:-@-@*J*3//'J4""//@@8899N  {{((I5$,,:R$$%WXX&*ll#{{55"kk<<%)[[%A%A(,(G(G$++ '3 '#=! .7-B))H\H\+4+@	mFYFY 5JJ
Oa,?@V\]KK00! 00P	!CRC^==?de"\\ "2^5I5I!5L"LUcUjUjN )33A6L(;;'))+%
 B%++A.+#11 &&
 7Id6H6H 	7
(&%#6%++A.	7
 	7
33  488]EXEXYZE[]_` %Q0D0J0J10MVjVqVq r3==a@/;;-0 
  7t66  
--, 
 	 
 !88q!"u9M_A'%++A.+$11 &&
 $## 

/-&%+)#6

 

 '$+
 	
r7   c                 .    | j                   j                  S r9   r  r!  rZ   s    r6   get_input_embeddingszBltModel.get_input_embeddings  s    !!...r7   c                 &    || j                   _        y r9   r  )r4   r   s     r6   set_input_embeddingszBltModel.set_input_embeddings  s    */'r7   r]  c                    |j                   d   }t        j                  t        j                  |d|j                  |j
                        |j                  d      d d d df   gd      }t        j                  ||j
                        }|j                  d      |j                  d      j                  d      k  j                  d      dz
  S )Nr   r   r=  rL   rv   r/  )
rY   rE   r  rB  rN   rp   cumsumr3  r   rk  )r4   r^  r]  r7  r  token_positionss         r6   r  z BltModel._patch_ids_from_lengths  s    "((+
yyJ1D1D]MaMab$$$,QV4 
  ,,w}7K7KL&&q)_-F-Fq-I-S-STV-WW\\ac\dghhhr7   rQ  )r<   r=   r>   r   r(   r   r   rE   r   r   r   r   r   r   r   r   r;   r  r  r   r  r?   r@   s   @r6   r  r    s+   y (  15041537+/59$(59~
E,,-~
  -~
 !.	~

 u//0~
 "%~
   1 12~
 D>~
 !!1!12~
 +,~
 
!~
 ~
@/0
iU\\ 
iC 
iTYT`T` 
ir7   r  zB
    The Blt Text Model with a language modeling head on top.
    )custom_introc            !           e Zd ZU eed<   dZdZdgZdef fdZe	e
	 	 	 	 	 	 	 	 	 	 	 	 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j                     dee   deej                     deeej                  f   dee   deeef   fd              Z xZS )BltForCausalLMr)   Fmodelzlm_head.weightc                 B   t         |   |j                                |j                         | _        |j                  | _        t        |      | _        t        j                  |j                  j                  |j                  d      | _        | j                          y r$   )r'   r(   get_text_configtext_configr   r  r  r,   r-   r  r*   ry  r%  r3   s     r6   r(   zBltForCausalLM.__init__  st    //12!113 ++f%
yy!6!6!B!BFDUDU\abr7   r(  r   r   r   r   r   r   r)  labelsr   r   logits_to_keepr   r   c                     | j                   d||||||||
|d	|}|j                  }t        |t              rt	        | d      n|}| j                  |dd|ddf         j                         }d}|	 | j                  ||	| j                  fi |}t        |||j                  |j                  |j                        S )a  
        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.
        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
        full_text_row_masked_out_mask (`tuple[torch.Tensor, torch.Tensor]`, *optional*):
            A tuple containing two tensors that mask out rows in the cross-attention mechanism:
            - The first tensor has shape `(batch_size, 1, seq_length, 1)` and contains values of 0 or 1.
              A value of 0 indicates that the corresponding text token's entire row in the cross-attention
              matrix should be masked out (all image tokens ignored).
            - The second tensor has the same shape and is used internally to apply the masking during
              the forward pass of cross-attention layers.
            This mask is derived from the cross_attention_mask and is used to handle cases where a text token
            should not attend to any image token.
        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]`.

        Example:

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

        >>> model = BltForCausalLM.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
        ```
        )	r(  r   r   r   r   r   r)  r   r   N)lossrP  r   rT   r  r   )r  r  rf   r   slicery  rx   loss_functionr   r   r   rT   r  )r4   r(  r   r   r   r   r   r   r)  r  r   r   r  r   outputsrT   slice_indicesrP  r  s                      r6   r;   zBltForCausalLM.forward  s    ~ $** 
)%!5*G+')
 
  118B>SV8W~ot4]kmA}a,?@AGGI%4%%ffdooPPD%#33!//))
 	
r7   )NNNNNNNNNNNr   )r<   r=   r>   r   r   r  r  _tied_weights_keysr(   r   r   r   rE   r   r   rX   r   r   listr   r   r   r   r   r   r;   r?   r@   s   @r6   r  r    s    "*+y   151537=A;?UYKO59-1$(5934X
E,,-X
 !.X
 u//0	X

 !))9)9 :X
 'u'7'78X
 (0ellELL6P0Q'RX
 "%tE4E4E/F(F"GHX
   1 12X
 ))*X
 D>X
 !!1!12X
 c5<</0X
 +,X
 
u,,	-X
  X
r7   r  )r   r  rw  r  )r   )Nr   )r  )rK   r  i0u  )Stypingr   r   r   rE   torch.distributionstorch.nnr,   torch.nn.functionalr   r2  activationsr   cache_utilsr   r	   
generationr
   masking_utilsr   modeling_flash_attention_utilsr   modeling_layersr   modeling_outputsr   r   modeling_rope_utilsr   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   utils.deprecationr   utils.genericr   r   configuration_bltr   r   r   r   r    Moduler"   rB   r]   r   r   r   r   rx   r   r   r   r   r   r   r  rI  rS  ru  rw  r  r  r  r  r  rP   r   rN   rX   r  r  r  __all__r   r7   r6   <module>r     s)  , - ,      ! . ) / B 9 O K F & I I 0 ? RYY "J J(!< !<JF4 FR	UU\\ 	U# 	U%,, 	U& %II%<<% 
% <<	%
 U\\*% % % '(%46D)ryy D)NL)		 L)^    p"( p"fL( L^2- 2j) )RU )[`[g[g )XF# FR: :< \a||),9<UX$#,,# !## +.	#
 #'# $'# \\#T  %K ||K K  K  	K 
 K  ;;K  5<<%&K \fi! fiR 
i
' i

i
X Mr7   