
    <hQ                     v   S SK r S SKJr  S SKJrJr  S SKrS SKJr  S SK	Js  J
r  S SKrSSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJrJr  SSKJrJ r J!r!J"r"J#r#  SSK$J%r%  SSK&J'r'J(r(J)r)  \RT                  " \+5      r, " S S\5      r- " S S\ 5      r. " S S\R^                  5      r0 " S S\5      r1 " S S\R^                  5      r2 " S S\R^                  5      r3 " S S\R^                  5      r4 " S S \R^                  5      r5 " S! S"\R^                  5      r6 " S# S$\R^                  5      r7 " S% S&\R^                  5      r8 " S' S(\%5      r9 " S) S*\Rt                  5      r; " S+ S,\R^                  5      r< " S- S.\R^                  5      r= " S/ S0\R^                  5      r> " S1 S2\R^                  5      r? " S3 S4\R^                  5      r@\" S5S69 " S7 S8\5      5       rA " S9 S:5      rB " S; S<\\A5      rC " S= S>\"\C5      rD " S? S@\!\C\5      rE " SA SB\C5      rF " SC SD\C\5      rG/ SEQrHg)F    N)cached_property)OptionalUnion   )Cache)GenerationMixin)CausalLMOutputWithPast)PreTrainedModel)Unpack)auto_docstringcan_return_tuplelogging   )ChameleonPreTrainedModel#ChameleonVQVAEEncoderConvDownsample)LlamaAttentionLlamaDecoderLayerLlamaForCausalLM
LlamaModelTransformersKwargs)SiglipAttention   )
Emu3ConfigEmu3TextConfigEmu3VQVAEConfigc                       \ rS rSrSrg)Emu3Attention,    N__name__
__module____qualname____firstlineno____static_attributes__r       ]/var/www/html/shao/venv/lib/python3.13/site-packages/transformers/models/emu3/modular_emu3.pyr   r   ,       r&   r   c                   t  ^  \ rS rSrS\S\4U 4S jjr      SS\R                  S\	\R                     S\	\R                     S\	\   S	\	\   S
\	\R                     S\	\\R                  \R                  4      S\\   S\\R                   \	\\R                   \R                   4      4   4S jjrSrU =r$ )Emu3DecoderLayer1   config	layer_idxc                 n   > [         TU ]  X5        [        R                  " UR                  5      U l        g N)super__init__nnDropoutattention_dropoutdropoutselfr,   r-   	__class__s      r'   r1   Emu3DecoderLayer.__init__2   s&    +zz&":":;r&   hidden_statesattention_maskposition_idspast_key_value	use_cachecache_positionposition_embeddingskwargsreturnc                     Un	U R                  U5      nU R                  " SUUUUUUUS.UD6u  pXR                  U5      -   nUn	U R                  U5      nU R	                  U5      nXR                  U5      -   nU$ )N)r:   r;   r<   r=   r>   r?   r@   r   )input_layernorm	self_attnr5   post_attention_layernormmlp)r7   r:   r;   r<   r=   r>   r?   r@   rA   residual_s              r'   forwardEmu3DecoderLayer.forward6   s     !,,];>> 	
')%)) 3	
 	
 !<<#>> 55mD/ <<#>>r&   )r5   )NNNFNN)r!   r"   r#   r$   r   intr1   torchTensorr   
LongTensorr   booltupler   r   FloatTensorrJ   r%   __classcell__r8   s   @r'   r*   r*   1   s    <z <c < 2637*.$)59KO|| !. u//0	
 ! D> !!1!12 &eELL%,,,F&GH +, 
u  (51B1BEDUDU1U+V"WW	X r&   r*   c                   V   ^  \ rS rSrSrS\4U 4S jjrS\R                  4S jr	Sr
U =r$ )Emu3VQVAEVectorQuantizerW   a  
A module for vector quantization using learned embedding vectors.

This module implements the quantization process similar to te one described in
the VQ-VAE (Vector Quantized Variational AutoEncoder) paper. It quantizes continuous
input vectors into discrete codebook vectors, which are learned during training.
Current implementation improves over previous ones by avoiding costly matrix multiplications
and allowing for post-hoc remapping of indices.
r,   c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        U R                  R                  R                  R                  SUR                  -  SUR                  -  5        g )Ng            ?)
r0   r1   r2   	Embeddingcodebook_size	embed_dim	embeddingweightdatauniform_r7   r,   r8   s     r'   r1   !Emu3VQVAEVectorQuantizer.__init__b   sb    f&:&:F<L<LM""++D63G3G,GvOcOcIcdr&   hidden_statec                    UR                   u  p#pEnUR                  SSSSS5      R                  5       nUR                  SU5      n[        R
                  " US-  SSS9n[        R
                  " U R                  R                  S-  SS	9n	S[        R                  " XpR                  R                  R                  SS5      5      -  n
X-   U
-
  n
[        R                  " U
SS	9nUR                  X#XV5      nU$ )
Nr   r   r      r   T)dimkeepdimrg   )shapepermute
contiguousviewrM   sumr]   r^   matmul	transposeargmin)r7   rc   
batch_sizetemporalchannelsheightwidthhidden_state_flattenedhidden_state_sumembedding_sum	distancesmin_encoding_indicess               r'   rJ    Emu3VQVAEVectorQuantizer.forwardg   s    8D8J8J5
h#++Aq!Q:EEG!-!2!22x!@ !99%;Q%>AtT		$.."7"7":B %;^^=R=R=\=\]^`a=bcc	$4y@	$||I1=388v]##r&   )r]   )r!   r"   r#   r$   __doc__r   r1   rM   rN   rJ   r%   rS   rT   s   @r'   rV   rV   W   s+    e e
$ELL $ $r&   rV   c                       \ rS rSrSrg)Emu3VQVAEEncoderConvDownsampley   r   Nr    r   r&   r'   r   r   y   r(   r&   r   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )Emu3VQVAEEncoderConvUpsample}   c                 Z   > [         TU ]  5         [        R                  " XSSSS9U l        g )Nr   r   kernel_sizestridepadding)r0   r1   r2   Conv2dconv)r7   in_channelsr8   s     r'   r1   %Emu3VQVAEEncoderConvUpsample.__init__~   s%    IIkAaYZ[	r&   c                 T    [         R                  " USSS9nU R                  U5      nU$ )N       @nearestscale_factormode)Finterpolater   r7   r:   s     r'   rJ   $Emu3VQVAEEncoderConvUpsample.forward   s(    m#IV		-0r&   r   r!   r"   r#   r$   r1   rJ   r%   rS   rT   s   @r'   r   r   }   s    \ r&   r   c            	       j   ^  \ rS rSrS\S\S\\   S\\   4U 4S jjrS\R                  4S jr	S	r
U =r$ )
Emu3VQVAEConv3d   
in_channelout_channelr   r   c                 P  > [         T	U ]  5         [        USS  USS  5       VVs/ sH	  u  pVXV-
  PM     nnnSU l        US S S2    H&  nU =R                  US-  US-  -   US-  4-  sl        M(     U =R                  S-  sl        [        R
                  " UUUUS9U l        g s  snnf )Nr   r   rf   r   )r   r   )r   )r0   r1   zipr   r2   Conv3dr   )
r7   r   r   r   r   
one_kernel
one_stridepadding_sizespad_sizer8   s
            r'   r1   Emu3VQVAEConv3d.__init__   s     	ORS^_`_aSbdjklkmdnOopOo5KZ0Oop%dd+HLLX]X\98q=IIL ,II	
	 qs   B"r:   c                 h    [         R                  " XR                  5      nU R                  U5      nU$ r/   )r   padr   r   r   s     r'   rJ   Emu3VQVAEConv3d.forward   s(    m\\:		-0r&   )r   r   )r!   r"   r#   r$   rL   rQ   r1   rM   rN   rJ   r%   rS   rT   s   @r'   r   r      sK    

 
 3Z	

 c

,U\\  r&   r   c                   n   ^  \ rS rSrS\S\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	Emu3VQVAESpatialNorm   r   out_channelsc                    > [         TU ]  5         [        R                  " USSSS9U l        [        R
                  " UUSSSS9U l        [        R
                  " UUSSSS9U l        g )N    ư>Tnum_channels
num_groupsepsaffiner   r   r   )r0   r1   r2   	GroupNorm
norm_layerr   conv_yconv_br7   r   r   r8   s      r'   r1   Emu3VQVAESpatialNorm.__init__   sn    
 	,,%	
 ii
 ii
r&   r:   quant_statesc                     [         R                  " X!R                  SS  SS9nU R                  U5      nXR	                  U5      -  U R                  U5      -   nU$ )Nr   )sizer   )r   r   rj   r   r   r   )r7   r:   r   s      r'   rJ   Emu3VQVAESpatialNorm.forward   sT    }}\8K8KBC8PW`a6%L(AADKKP\D]]r&   )r   r   r   r!   r"   r#   r$   rL   r1   rM   rN   rJ   r%   rS   rT   s   @r'   r   r      s:    

 
8U\\   r&   r   c                   V   ^  \ rS rSrS\S\4U 4S jjrS\R                  4S jrSr	U =r
$ )Emu3VQVAETemporalUpsample   r   r   c                 D   > [         TU ]  5         [        UUSSS9U l        g )Nr   r   r   r   r   r   r   r   r0   r1   r   r   r7   r   r   r8   s      r'   r1   "Emu3VQVAETemporalUpsample.__init__   (    
 	#!	
	r&   r:   c                 D   UR                   u  p#pEnUR                  SSSSS5      R                  5       R                  USU5      n[        R
                  " USSS	9nUR                  X#XVS5      R                  SSSSS5      R                  5       nU R                  U5      nU$ )
Nr   r   r   re   r   rf   r   r   r   )rj   rk   rl   rm   r   r   r   )r7   r:   rr   rt   rs   ru   rv   s          r'   rJ   !Emu3VQVAETemporalUpsample.forward   s    8E8K8K5
h%--aAq!<GGINNz[]_ghm#IV%**:PRS[[\]_`bcefhijuuw		-0r&   r   r   rT   s   @r'   r   r      s/    

 
U\\  r&   r   c                   V   ^  \ rS rSrS\S\4U 4S jjrS\R                  4S jrSr	U =r
$ )Emu3VQVAETemporalDownsample   r   r   c                 D   > [         TU ]  5         [        UUSSS9U l        g )N)re   r   r   )r   r   r   r   r   r   s      r'   r1   $Emu3VQVAETemporalDownsample.__init__   r   r&   r:   c                 (    U R                  U5      nU$ r/   r   r   s     r'   rJ   #Emu3VQVAETemporalDownsample.forward   s    		-0r&   r   r   rT   s   @r'   r   r      s/    

 
U\\  r&   r   c                   4   ^  \ rS rSr SU 4S jjrS rSrU =r$ )Emu3VQVAETemporalResnetBlock   c                 f  > [         TU ]  5         Xl        Uc  UOUU l        [        R
                  " U5      U l        [        UUSSS9U l        [        R
                  " U5      U l	        [        UUSSS9U l
        U R                  U R                  :w  a  [        R                  " UUSSSS9U l        g g )Nr   r   r   r   r   r   )r0   r1   r   r   r2   BatchNorm3dnorm1r   conv1norm2conv2r   nin_shortcutr   s      r'   r1   %Emu3VQVAETemporalResnetBlock.__init__   s    
 	&+7+?K\^^K0
$!	

 ^^L1
$!	

 t000 "		!D 1r&   c                 P   UnU R                  U5      nU[        R                  " U5      -  nU R                  U5      nU R	                  U5      nU[        R                  " U5      -  nU R                  U5      nU R                  U R                  :w  a  U R                  U5      nX!-   $ r/   )	r   rM   sigmoidr   r   r   r   r   r   )r7   r:   rH   s      r'   rJ   $Emu3VQVAETemporalResnetBlock.forward  s     

=1}55

=1

=1}55

=1t000((2H''r&   )r   r   r   r   r   r   r   r/   r   rT   s   @r'   r   r      s     @( (r&   r   c                      ^  \ rS rSr  S	S\S\\   S\\   4U 4S jjjrS
S\R                  S\\R                     4S jjr	Sr
U =r$ )Emu3VQVAEResnetBlocki$  r   r   quant_channelsc                   > [         TU ]  5         Xl        Uc  UOUnX l        X0l        Uc9  [
        R                  " USSSS9U l        [
        R                  " USSSS9U l        O [        X15      U l        [        X25      U l        [
        R                  " UUSSSS9U l        [
        R                  " UUSSSS9U l        U R                  U R                  :w  a  [
        R                  " UUSSSS9U l        g g )	Nr   r   Tr   r   r   r   r   )r0   r1   r   r   r   r2   r   r   r   r   r   r   r   r   )r7   r   r   r   r8   s       r'   r1   Emu3VQVAEResnetBlock.__init__%  s     	&&2&:{(,!;2SW`deDJ<BTXaefDJ-nJDJ-nKDJYY

 YY

 t000 "		!D 1r&   r:   c                 |   U R                   c  SOU4nUnU R                  " U/UQ76 nU[        R                  " U5      -  nU R	                  U5      nU R
                  " U/UQ76 nU[        R                  " U5      -  nU R                  U5      nU R                  U R                  :w  a  U R                  U5      nXA-   $ Nr   )
r   r   rM   r   r   r   r   r   r   r   )r7   r:   r   	norm_argsrH   s        r'   rJ   Emu3VQVAEResnetBlock.forwardQ  s    --5BN;L	 

==9=}55

=1

==9=}55

=1t000((2H''r&   )r   r   r   r   r   r   r   r   )NNr/   )r!   r"   r#   r$   rL   r   r1   rM   rN   rJ   r%   rS   rT   s   @r'   r   r   $  s_     '+(,	** sm* !	* *X(U\\ (8ELLCY ( (r&   r   c                   0   ^  \ rS rSrS\4U 4S jjrSrU =r$ )Emu3VQVAEAttentionBlockic  r,   c                 2   > [         TU ]  U5        SU l        g )Nr   )r0   r1   num_key_value_groupsra   s     r'   r1    Emu3VQVAEAttentionBlock.__init__d  s      %&!r&   )r   )r!   r"   r#   r$   r   r1   r%   rS   rT   s   @r'   r   r   c  s    & & &r&   r   c                   6   ^  \ rS rSrSrU 4S jrSS jrSrU =r$ )Emu3VQVAEGroupNormik  z
Same as the torch GroupNorm with the only difference that this ones accepts
an optional kwarg `quant_states` which is not used. This class makes it easier to
use SpatialNorm or GroupNorm without conditionals
c                 &   > [         TU ]  " S0 UD6  g r   )r0   r1   )r7   rA   r8   s     r'   r1   Emu3VQVAEGroupNorm.__init__r  s    "6"r&   c                     [         R                  " XR                  U R                  U R                  U R
                  5      $ r/   )r   
group_normr   r^   biasr   )r7   inputr   s      r'   rJ   Emu3VQVAEGroupNorm.forwardu  s'    ||E??DKKDHHUUr&   r   r/   )	r!   r"   r#   r$   r}   r1   rJ   r%   rS   rT   s   @r'   r   r   k  s    #V Vr&   r   c                   p   ^  \ rS rSrSU 4S jjrSS\R                  S\\R                     4S jjrSr	U =r
$ )Emu3VQVAEMiddleBlockiy  c                    > [         TU ]  5         [        UUUS9U l        [	        U5      U l        Uc  [        USSSS9U l        O[        X25      U l        [        UUUS9U l	        g )Nr   r   r   r   r   Tr   )
r0   r1   r   block_1r   attn_1r   	attn_normr   block_2)r7   r,   r   r   r8   s       r'   r1   Emu3VQVAEMiddleBlock.__init__z  sm    +#$)

 .f5!/[UW]ajnoDN1.NDN+#$)
r&   r:   r   c                 N   U R                  X5      nUnU R                  X5      nUR                  u  pEpgUR                  XEXg-  5      R	                  SS5      nU R                  U5      S   nUR                  XFXu5      R                  SSSS5      nX1-   nU R                  X5      nU$ )Nr   r   r   r   )	r   r   rj   rm   rp   r   reshaperk   r  )r7   r:   r   rH   rr   rt   ru   rv   s           r'   rJ   Emu3VQVAEMiddleBlock.forward  s    ]A }C.;.A.A+
f%**:PZZ[\^_`M215%--j%RZZ[\^_abdef 0]Ar&   )r   r   r   r  r/   )r!   r"   r#   r$   r1   rM   rR   r   rJ   r%   rS   rT   s   @r'   r   r   y  s1    
(
U%6%6 
huO`O`Fa 
 
r&   r   c                   J   ^  \ rS rSrU 4S jrS\R                  4S jrSrU =r	$ )Emu3VQVAEDownBlocki  c                   > [         TU ]  5         [        UR                  5      U l        UR
                  U l        UR                  nUR                  nS[        U5      -   nX@l        [        R                  " 5       U l        [        U R                  5       GHL  n[        R                  " 5       n[        R                  " 5       n[        R                  " 5       nX$U   -  n	X#U   -  n
[        U R
                  5       H~  nUR                  [        U	U
S95        U
n	UR                  c  M-  XQR                  ;   d  M>  UR                  [!        U5      5        UR                  [        R"                  " U	SSSS95        M     [        R$                  " 5       nXll        X|l        Xl        XPR                  S-
  :w  a  [-        U	5      Ul        U R                  R                  U5        GMO     g )N)r   r   r   r   r   Tr   r   )r0   r1   lenchannel_multipliernum_resolutionsnum_res_blocksbase_channelsrQ   in_channel_multiplierr2   
ModuleListdownrangeappendr   attn_resolutionsr   r   Moduleblockattn
attn_normsr   
downsample)r7   r,   r  r  r  i_levelr  r  r  block_in	block_outi_blockr  r8   s                r'   r1   Emu3VQVAEDownBlock.__init__  s   "6#<#<=$33,,#66 $u-?'@ @%:"MMO	T112GMMOE==?DJ$W'EEH%7(CCI !4!45($,%. %**67F]F];]KK 7 ?@%%bllUW]ajn&op 6 99;DJI(O..22"@"JIIT"1 3r&   r:   c                 <   [        U R                  5       GH  u  p#[        U R                  5       H  nUR                  U   " U5      n[        UR                  5      S:  d  M3  UnUR                  U   " U5      nUR                  u  pgpUR                  XgX-  5      R                  SS5      nUR                  U   " U5      S   nUR                  XhX5      R                  SSSS5      nXQ-   nM     X R                  S-
  :w  d  M  UR                  U5      nGM     U$ )Nr   r   r   r   )	enumerater  r  r  r  r
  r  r  rj   rm   rp   r  rk   r  r  )
r7   r:   r  blocksr  rH   rr   rt   ru   rv   s
             r'   rJ   Emu3VQVAEDownBlock.forward  s   (3OG !4!45 &W 5m Dv{{#a',H$*$5$5g$>}$MM:G:M:M7J&$1$6$6zV^$\$f$fghjk$lM$*KK$8$G$JM$1$9$9*e$^$f$fghjkmnpq$rM$,$<M 6 ..22 & 1 1- @  4" r&   )r  r  r  r  
r!   r"   r#   r$   r1   rM   rR   rJ   r%   rS   rT   s   @r'   r  r    s     ##JU%6%6  r&   r  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )Emu3VQVAEUpBlocki  c           
        > [         TU ]  5         [        UR                  5      U l        UR
                  U l        UR                  nUR                  UR                  S   -  n[        R                  " 5       U l
        [        [        U R                  5      5       GH8  n[        R                  " 5       n[        R                  " 5       n[        R                  " 5       nUR                  UR                  U   -  n[        U R
                  S-   5       Hd  n	UR                  [        UUUS95        UnXAR                  ;   d  M0  UR                  [!        U5      5        UR                  [#        X#5      5        Mf     [        R$                  " 5       n
XZl        Xjl        Xzl        US:w  a  [-        U5      U
l        U R                  R1                  SU
5        GM;     g )Nrf   r   r   r   )r0   r1   r
  r  r  r  r\   r  r2   r  upreversedr  r  r   r  r   r   r  r  r  r  r   upsampleinsert)r7   r,   r   r  r  r  r  r  r  r  r'  r8   s              r'   r1   Emu3VQVAEUpBlock.__init__  si   "6#<#<=$33))''&*C*CB*GG--/d&:&: ;<GMMOE==?DJ,,v/H/H/QQI !4!4q!89($,%.'5 %555KK 7 ?@%%&:>&TU : BHG&M!|:8DGGNN1b!3 =r&   r:   r   c                 b   [        U R                  S S S2   5       GH  u  p4[        U R                  S-   5       H  nUR                  U   " X5      n[        UR                  5      S:  d  M3  UnUR                  U   " X5      nUR                  u  pxpUR                  XxX-  5      R                  SS5      nUR                  U   " U5      S   nUR                  XyX5      R                  SSSS5      nXa-   nM     U[        U R                  5      S-
  :w  d  M  UR                  U5      nGM     U$ )Nrf   r   r   r   r   )r   r'  r  r  r  r
  r  r  rj   rm   rp   r  rk   r)  )r7   r:   r   r  r!  r  rH   rr   rt   ru   rv   s              r'   rJ   Emu3VQVAEUpBlock.forward  s   (27OG !4!4q!89 &W 5m Rv{{#a',H$*$5$5g$>}$[M:G:M:M7J&$1$6$6zV^$\$f$fghjk$lM$*KK$8$G$JM$1$9$9*e$^$f$fghjkmnpq$rM$,$<M : #dgg,** & >  8  r&   )r  r  r'  r#  rT   s   @r'   r%  r%    s-    #"JU%6%6 eFWFW  r&   r%  c                   J   ^  \ rS rSrU 4S jrS\R                  4S jrSrU =r	$ )Emu3VQVAEEncoderi  c                   > [         TU ]  5         UR                  nUR                  nUR                  nUR
                  nUR                  nU(       a  SU-  OUnX&S   -  n[        R                  R                  X2SSSS9U l
        [        U5      U l        [        X5      U l        [        R                  R                  SUSSS	9U l        [        R                  R                  UUSSSS9U l        [%        [&        R(                  " UR*                  5      5      n	[        R,                  " 5       U l        [        R,                  " 5       U l        [3        U	5       H)  n
[5        Xw5      nU R.                  R7                  U5        M+     [3        UR8                  5       H(  n[;        UUS
9nU R0                  R7                  U5        M*     g )Nr   rf   r   r   r   r   r   T)r   r   r   r   r	  )r0   r1   r  r   double_latentlatent_channelsr  rM   r2   r   conv_inr  
down_blockr   middle_blockr   norm_outconv_outrL   mathlog2temporal_downsample_factorr  	time_convtime_res_stackr  r   r  r  r   )r7   r,   r  r   r1  r2  r  r   r  temporal_down_blocksir   rI   time_res_convr8   s                 r'   r1   Emu3VQVAEEncoder.__init__  s   ,,((,, 00#66.;q?* b#99xx{qYZdef,V40B**bxUYbf*g ( 
  #499V-N-N#OP mmo+,A.|JDNN!!$' - v,,-A8()M &&}5 .r&   pixel_valuesc                 t   UR                   S   nUR                  " S/UR                   SS  Q76 nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU[        R                  " U5      -  nU R                  U5      nUR                  " SU/UR                   SS  Q76 nUR                  SSSSS5      nU R                   H$  nU" U5      nU[        R                  " U5      -  nM&     U R                   H  nU" U5      nM     UR                  SSSSS5      nU$ )Nr   rf   r   r   r   re   )rj   r  r3  r4  r5  r6  rM   r   r7  rk   r;  r<  )r7   rA  temporal_dimr:   r   layers         r'   rJ   Emu3VQVAEEncoder.forward8  s:   #))!,#++BH1C1CAB1GH \26))-8 m4}55m4%--b,YATATUVUWAXY%--aAq!< NND /MU]]=99M # ((E!-0M ) &--aAq!<r&   )r3  r7  r4  r5  r6  r;  r<  )
r!   r"   r#   r$   r1   rM   rO   rJ   r%   rS   rT   s   @r'   r/  r/    s     %6NE$4$4  r&   r/  c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )Emu3VQVAEDecoderiV  r,   c                   > [         T	U ]  5         UR                  nUR                  UR                  S   -  n[
        R                  " 5       U l        [        UR                  5       H<  n[        UR                  UR                  S9nU R                  R                  U5        M>     [        [        R                  " UR                   5      5      n[
        R                  " 5       U l        [        U5       H>  n[%        UR                  UR                  5      nU R"                  R                  U5        M@     [
        R&                  " UR                  USSSS9U l        [+        XUS9U l        [/        U5      U l        UR                  UR                  S   -  n[3        X#5      U l        [
        R&                  " UUR6                  SSSS9U l        g )Nrf   r	  r   r   r   )r   r   )r0   r1   r\   r  r  r2   r  r<  r  r  r   r2  r  rL   r8  r9  r:  r;  r   r   r3  r   r5  r%  up_blockr   r6  r   r7  )
r7   r,   r   r  rI   r?  temp_upsample_block_numr>  r   r8   s
            r'   r1   Emu3VQVAEDecoder.__init__W  s|   ))''&*C*CB*GG mmov,,-A8"22AWAWM &&}5	 . #&dii0Q0Q&R"S./A,V-C-CVE[E[\DNN!!$' 0 yy""
 1R`a(0''&*C*CA*FF,^F		
r&   r:   r   c                    [         R                  " X4SS9nUR                  SSSSS5      nU R                   H  nU" U5      nM     U R                   H$  nU" U5      nU[         R
                  " U5      -  nM&     UR                  SSSSS5      n[         R                  " USSS9u  pUR                  " S/UR                  SS  Q76 nUR                  " S/UR                  SS  Q76 nU R                  U5      nU R                  X5      nU R                  X5      nU R                  X5      nU[         R
                  " U5      -  nU R                  U5      nU$ )Nr   ri   r   r   r   re   rf   )rM   catrk   r<  r;  r   chunkr  rj   r3  r5  rI  r6  r7  )r7   r:   r   hidden_quant_statesrD  s        r'   rJ   Emu3VQVAEDecoder.forward~  sV   #ii(E1M199!Q1aH ((E"'(;"< ) ^^E"'(;"<5==1D#EE $ 299!Q1aH&+kk2Eqa&P#%--bK=3F3Fqr3JK#++BH1C1CAB1GH]3 ))-FmBmB}55m4r&   )r3  r7  r5  r6  r;  r<  rI  )r!   r"   r#   r$   r   r1   rM   rN   rJ   r%   rS   rT   s   @r'   rG  rG  V  s0    %
 %
NU\\   r&   rG  aR  
    The VQ-VAE model used in Emu3 for encoding/decoding images into discrete tokens.
    This model follows the "Make-a-scene: Scene-based text-to-image generation with human priors" paper from
    [ Oran Gafni, Adam Polyak, Oron Ashual, Shelly Sheynin, Devi Parikh, and Yaniv
    Taigman](https://huggingface.co/papers/2203.13131).
    )custom_introc                      ^  \ rS rSr% \\S'   SrSrSrSr	Sr
Sr/ SQrS rS\4U 4S jjrS\R                   S	\R                   4S
 jrS\R                   4S jrSrU =r$ )	Emu3VQVAEi  r,   
emuvideovqrA  T)r   r   r   rV   c                    [        U[        R                  [        R                  45      (       a  [        R                  R                  UR                  SSS9  UR                  bq  [        R                  R                  UR                  5      u  p#S[        R                  " U5      -  n[        R                  R                  UR                  U* U5        g g [        U[        R                  5      (       a  [        R                  R                  UR                  [        R                  " S5      S9  UR                  by  [        R                  R                  UR                  5      u  p#US:  a  S[        R                  " U5      -  OSn[        R                  R                  UR                  U* U5        g g [        U[        R                  [        R                  [        R                   45      (       aU  [        R                  R#                  UR                  S5        [        R                  R#                  UR                  S	5        g [        U[        R$                  5      (       ad  UR                  R&                  R)                  5         UR*                  b2  UR                  R&                  UR*                     R-                  5         g g g )
Nfan_outrelu)r   nonlinearityr      )ar   rY   g        )
isinstancer2   r   r   initkaiming_normal_r^   r   _calculate_fan_in_and_fan_outr8  sqrtr`   Linearkaiming_uniform_BatchNorm2dr   r   	constant_rZ   r_   normal_padding_idxzero_)r7   modulefan_inrI   bounds        r'   _init_weightsEmu3VQVAE._init_weights  s   fryy"))455GG##FMM	PV#W{{&GGAA&--P	DIIf--  ufe< ' 		**GG$$V]]diil$C{{&GGAA&--P	17!DIIf--  ufe< '  NOOGGfmmS1GGfkk3/--MM&&(!!-""6#5#56<<> . .r&   c                   > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        [        U5      U l        S[        UR                  5      S-
  -  U l        [        UR                  UR                  SSS9U l        [        UR                  UR                  SSS9U l        S[        UR                  5      S-
  -  U l        U R%                  5         U R'                  5         g )Nr   r   )r   r   r   r   r   )r0   r1   r,   r/  encoderrG  decoderrV   quantizer
  r  vision_spatial_factorr   r2  r\   
quant_convpost_quant_convspatial_scale_factoreval	post_initra   s     r'   r1   Emu3VQVAE.__init__  s     '/'/08%&3v/H/H+IA+M%N")""F$4$4)T]
  /f44)T] 
 %&#f.G.G*H1*L$M!		r&   image_sizesc                    UR                   S:H  nU(       aJ  U R                  R                  nUR                  u  pVpxUR	                  S5      R                  SUSSS5      nOUR                  u  pTpgnU R                  U5      n	U	R                  SSSSS5      n	U R                  U	5      n	U	R                  SSSSS5      n	U R                  U	5      n
U(       a  U
R                  S5      OU
n[        X5       VVs/ sHB  u  pUS [        US   U R                  -  5      2S [        US   U R                  -  5      24   PMD     nnnU$ s  snnf )Nre   r   r   r   r   )ndimr,   r:  rj   	unsqueezerepeatrm  rk   rq  ro  squeezer   rL   rp  )r7   rA  rw  is_imagers   rr   rt   ru   rv   r:   codesimage_tokenssingle_imager   s                 r'   encodeEmu3VQVAE.encode  sP   $$){{==H2>2D2D/J&'11!4;;AxAqQL<H<N<N9J(E\2 &--aAq!<6 &--aAq!<m,+3u}}Q' '*,&D
&D" D3tAw)C)CCDDFqDQRGVZVpVpLpHqFqqr&D 	 

 
s   6AEr:   c                    UR                   S:H  nU(       a  UR                  S5      nUR                  u  p4pVU R                  R	                  UR                  5       5      nUR                  S   nUR                  X4XVU5      R                  SSSSS5      R                  5       nU R                  U5      n	UR                  SSSSS5      nU	R                  SSSSS5      n	U R                  X5      n
U
R                  UX@R                  R                  -  U R                  R                  XPR                  -  X`R                  -  5      n
U(       a	  U
S S 2S4   $ U
$ )Nr   r   rf   r   re   r   )ry  rz  rj   ro  r]   flattenrm   rk   rl   rr  rn  r  r,   r:  r   rs  )r7   r:   r}  rr   rs   ru   rv   quantrt   
post_quantvideos              r'   decodeEmu3VQVAE.decode  s;    %%*)33A6M.;.A.A+
f''(=(=(?@;;r?

:IQQRSUVXY[\^_`kkm))%0
aAq!,''1aA6
Z/{{===KK$$...---
 'uQT{1E1r&   )r,   rn  rm  rr  rq  ro  rs  rp  )r!   r"   r#   r$   r   __annotations__base_model_prefixmain_input_name_supports_sdpa_supports_flash_attn_supports_flex_attn_supports_attention_backend_no_split_modulesrj  r1   rM   rN   r  r  r%   rS   rT   s   @r'   rS  rS    sv     $$ON"&?* *5<< ell 82ELL 2 2r&   rS  c                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       rS
\\R                     S\R                  4S jrS
\R                  S\R                  4S jrSrg)Emu3ImageVocabularyMappingi  zE
A class for mapping discrete image tokens from VQGAN to BPE tokens.
c                 h    Xl         UR                  S5      U l        UR                  S5      U l        g )Nz<|extra_200|>z<image>)	vocab_mapgeteol_token_idimage_token_id)r7   r  s     r'   r1   #Emu3ImageVocabularyMapping.__init__  s)    "%MM/:'mmI6r&   c           	          [        U R                  R                  5        VVs/ sH  u  pUR                  S5      (       d  M  UPM!     snn5      $ s  snnf Nz<|visual tokensortedr  items
startswithr7   namevals      r'   r  'Emu3ImageVocabularyMapping.image_tokens  s<    DNN,@,@,Bh,BytdooVfFgs,Bhiih   A
A
c           	          [        U R                  R                  5        VVs/ sH  u  pUR                  S5      (       d  M  UPM!     snn5      $ s  snnf r  r  r  s      r'   image_tokens_str+Emu3ImageVocabularyMapping.image_tokens_str!  s<    T^^-A-A-Ci-C	tWgGht-Cijjir  c                 x    U R                    Vs0 sH  n[        USS 5      U R                  U   _M!     sn$ s  snf )Nir   )r  rL   r  )r7   tokens     r'   img2bpe"Emu3ImageVocabularyMapping.img2bpe%  s;    FJF[F[\F[UE"RL!4>>%#88F[\\\s   %7c                 j    U R                   R                  5        VVs0 sH  u  pX!_M	     snn$ s  snnf r/   )r  r  )r7   kvs      r'   bpe2img"Emu3ImageVocabularyMapping.bpe2img)  s-    !%!3!3!56!5!5666s   /c                     [         R                  " [        U R                  R	                  5       5      S-   [         R
                  S9nU R                  R                  5        H	  u  p#X1U'   M     U$ Nr   dtype)rM   zerosmaxr  keysrL   r  r7   mappingr  r  s       r'   bpe2img_mapping_tensor1Emu3ImageVocabularyMapping.bpe2img_mapping_tensor-  R    ++c$,,"3"3"56:%))LLL&&(DAAJ )r&   c                     [         R                  " [        U R                  R	                  5       5      S-   [         R
                  S9nU R                  R                  5        H	  u  p#X1U'   M     U$ r  )rM   r  r  r  r  rL   r  r  s       r'   img2bpe_mapping_tensor1Emu3ImageVocabularyMapping.img2bpe_mapping_tensor4  r  r&   	img_batchrB   c                 "   UR                   n[        R                  " UR                  S   S4[        R                  S9U R
                  -  nU R                  UR                  S5         n[        R                  " XC/SS9nUR                  U5      $ )Nr   r   r  cpurf   ri   )	devicerM   onesrj   rL   r  r  torM  )r7   r  r  eol_row
img_tokenss        r'   convert_img2bpe*Emu3ImageVocabularyMapping.convert_img2bpe;  su    !!**iooa0!4EIIFIZIZZ00e1DE
YY
4"=
}}V$$r&   c                     UR                   nUSS S24   nU R                  UR                  S5         nUR                  U5      $ )N.rf   r  )r  r  r  )r7   r  r  r  s       r'   convert_bpe2img*Emu3ImageVocabularyMapping.convert_bpe2imgB  sG    !!c3B3h'	00e1DE
}}V$$r&   )r  r  r  N)r!   r"   r#   r$   r}   r1   r   r  r  r  r  r  r  listrM   rN   r  r  r%   r   r&   r'   r  r    s    7
 j j k k ] ] 7 7    %ell); % %% %%,, %r&   r  c                   "    \ rS rSrS/rSrSrSrg)Emu3PreTrainedModeliI  r*   Tr   N)r!   r"   r#   r$   r  r  r  r%   r   r&   r'   r  r  I  s     "&r&   r  c                   :   ^  \ rS rSr\\S.rS\4U 4S jjrSr	U =r
$ )Emu3TextModeliQ  )r:   
attentionsr,   c           	         > [         TU ]  U5        [        R                  " [	        UR
                  5       Vs/ sH  n[        X5      PM     sn5      U l        g s  snf r/   )r0   r1   r2   r  r  num_hidden_layersr*   layersr6   s      r'   r1   Emu3TextModel.__init__W  sH     mmBGH`H`BabBaYf0Bab
bs   A)r  )r!   r"   r#   r$   r*   r   _can_record_outputsr   r1   r%   rS   rT   s   @r'   r  r  Q  s"    )#

z 
 
r&   r  c                   @   ^  \ rS rSr% \\S'   U 4S jrU 4S jrSrU =r	$ )Emu3ForCausalLMi^  r,   c                 D   > [         TU ]  U5        [        U5      U l        g r/   )r0   r1   r  modelra   s     r'   r1   Emu3ForCausalLM.__init__a  s     "6*
r&   c                  6   > [        5       R                  5         g)aI  
Example:

```python
>>> from transformers import Emu3Processor, Emu3ForConditionalGeneration
>>> import torch
>>> import requests
>>> from PIL import Image

>>> model = Emu3ForCausalLM.from_pretrained("BAAI/Emu3-Chat-hf", torch_dtype=torch.bfloat16)
>>> processor = Emu3Processor.from_pretrained("BAAI/Emu3-Chat-hf")

>>> inputs = processor(text=["Can you write me a poem about winter."], return_tensors="pt").to(model.device)

>>> generated_ids = model.generate(**inputs, max_new_tokens=100, do_sample=False)
>>> processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
```N)r0   rJ   )super_kwargsr8   s    r'   rJ   Emu3ForCausalLM.forwarde  s    $ 	r&   )r  )
r!   r"   r#   r$   r   r  r1   rJ   r%   rS   rT   s   @r'   r  r  ^  s    + r&   r  c                   v  ^  \ rS rSrSS0rU 4S jrS rS rS rS r	S	\
R                  S
\
R                  4S jrS	\
R                  S
\
R                  4S jr\
R                  S\
R                  S\S\4S j5       rS\
R                  S\
R                  S\
R                  4S jr\\         SS\
R                  S	\
R                  S
\
R*                  S\\
R*                     S\\
R                     S\\   S\\
R                     S\\   S\\
R                     S\\   S\\\4   4S jj5       5       rSrU =r $ )	Emu3Modeliz  ztext_model.model
text_modelc                    > [         TU ]  U5        [        R                  UR                  5      U l        [        UR                  5      U l        [        UR                  5      U l        U R                  5         g r/   )r0   r1   r  _from_configtext_configr  rS  	vq_configvqmodelr  vocabulary_mapvocabulary_mappingru  ra   s     r'   r1   Emu3Model.__init__}  sY     '44V5G5GH !1!12"<V=R=R"S 	r&   c                 6    U R                   R                  5       $ r/   )r  get_input_embeddingsr7   s    r'   r  Emu3Model.get_input_embeddings  s    3355r&   c                 :    U R                   R                  U5        g r/   )r  set_input_embeddingsr7   values     r'   r  Emu3Model.set_input_embeddings  s    ,,U3r&   c                     Xl         g r/   r  r7   rn  s     r'   set_decoderEmu3Model.set_decoder  s    !r&   c                     U R                   $ r/   r  r  s    r'   get_decoderEmu3Model.get_decoder  s    r&   rA  rw  c                     U R                   R                  X5      nU Vs/ sH+  o@R                  R                  U5      R	                  5       PM-     nn[
        R                  " U5      nU$ s  snf )a  
Tokenizes images into discrete tokens with VQGAN module. Converts
obtained image tokens into BPE tokens and wraps with "boi" and "eoi"
special tokens.

Args:
    pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
        The tensors corresponding to the input images.
    image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`):
        The sizes of the images in the batch, being (height, width) for each image.
)r  r  r  r  r  rM   rM  )r7   rA  rw  image_tokens_listtokensbpe_tokens_list
bpe_tokenss          r'   get_image_tokensEmu3Model.get_image_tokens  sb     !LL//JctuctY_22BB6JRRTctuYY/
 vs   1A+c                    U R                  X5      nU VVs/ sH9  u  pEX@R                  R                  -  XPR                  R                  -  S-   -  PM;     nnnU R                  5       " U5      n[        R
                  " Xv5      nU$ s  snnf )a  
Tokenizes images into discrete tokens with VQGAN module and embeds
them with text embeddings layer

Args:
    pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)):
        The tensors corresponding to the input images.
r   )r  r  rp  r  rM   split)r7   rA  rw  r  ru   rv   split_sizesimage_featuress           r'   get_image_featuresEmu3Model.get_image_features  s     ,,\G "-
!, ||999e||GiGi>ilm>mn!, 	 
 224\B^A
s   ?Br  ru   rv   c                     USS2SS24   R                  SX#S-   5      nU R                  R                  U5      nU R                  R	                  U5      nU$ )a  
Decodes generated image tokens from language model to continuous pixel values
with VQGAN module via upsampling.

Args:
    image_tokens (`torch.LongTensor` of shape `(batch_size, num_of_tokens)`):
        The tensors corresponding to the input images.
    height (`int`):
        Height of the generated image before upsampling.
    width (`int`):
        Width of the generated image before upsampling.
Nrf   r   )rm   r  r  r  r  )r7   r  ru   rv   	sequencesimages         r'   decode_image_tokensEmu3Model.decode_image_tokens  sV     !CRC(--b&!)D	..>>yI##L1r&   	input_idsinputs_embedsr  c           	      J   Ucj  X R                  5       " [        R                  " U R                  R                  [        R
                  UR                  S95      :H  nUR                  S5      nOXR                  R                  :H  nUR                  5       nUR                  S5      R                  U5      R                  UR                  5      nUR                  S   UR                  S   -  nX$   R                  5       UR                  5       :w  a  [        SU SU 35      eU$ )z
Obtains multimodal placeholdr mask from `input_ids` or `inputs_embeds`, and checks that the placeholder token count is
equal to the length of multimodal features. If the lengths are different, an error is raised.
)r  r  rf   r   r   z6Image features and image tokens do not match: tokens: z, features )r  rM   tensorr  r  longr  allrn   rz  	expand_asr  rj   numel
ValueError)r7   r  r  r  special_image_maskn_image_tokensn_image_featuress          r'   get_placeholder_maskEmu3Model.get_placeholder_mask  s    !.2K2K2MT44CC5::^k^r^rs3 " "4!7!7!;!*.E.E.T.T!T+//1/99"=GGVYYZgZnZno)//2^5I5I!5LL,2248L8L8NNHHXXcdtcuv  "!r&   r;   r<   past_key_valuesr>   r?   rA   rB   c
           
      0   USL USL-  (       a  [        S5      eUc  U R                  5       " U5      nUbG  U R                  X#5      n[        R                  " USS9nU R                  XUS9nUR                  X5      nU R                  " SUUUUUU	S.U
D6nU$ )aH  
image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`):
    The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using
    [`AutoImageProcessor`]. See [`Emu3ImageProcessor.__call__`] for details ([]`Emu3Processor`] uses
    [`Emu3ImageProcessor`] for processing images).
NzaYou cannot specify both input_ids and inputs_embeds at the same time, and must specify either oner   ri   )r  r  )r;   r<   r  r  r>   r?   r   )r  r  r  rM   rM  r  masked_scatterr  )r7   r  rA  rw  r;   r<   r  r  r>   r?   rA   image_embedsr  outputss                 r'   rJ   Emu3Model.forward  s    * -t";<s    557	BM#22<ML 99\q9L!%!:!:| "; " *889KZM // 
)%+')
 
 r&   )r  r  r  )	NNNNNNNNN)!r!   r"   r#   r$   _checkpoint_conversion_mappingr1   r  r  r  r  rM   rR   rO   r  r  no_gradrL   r  r  r   r   rN   r   r   rP   r   r   r   rQ   r	   rJ   r%   rS   rT   s   @r'   r  r  z  s   &8,%G"64"U->-> UM]M] "u/@/@ uO_O_ $ ]]0@0@ # VY  $"))":?:K:K"]b]n]n"0  '+*.$(1537+/59$(59.##. ''. \\	.
 !.. u//0. "%.   1 12. D>. !!1!12. +,. 
u,,	-.  .r&   r  c                   0  ^  \ rS rSrSrS/rSSSS.rU 4S jrS	 rS
 r	S\
R                  4S jrS rS r\S 5       r\S 5       r\S 5       rS r\\           S"S\R.                  S\R0                  S\R2                  S\\R2                     S\\R.                     S\\   S\\R0                     S\\   S\\R.                     S\\R.                     S\\\R2                  4   S\\    S\\!\"4   4S jj5       5       r#       S#U 4S  jjr$S!r%U =r&$ )$Emu3ForConditionalGenerationi   zlm_head.weightzmodel.text_modelzmodel.vqmodellm_head)z^text_model.modelz^vqmodelz^text_model.lm_headc                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  R                  UR                  R                  SS9U l	        U R                  5         g )NF)r   )r0   r1   r  r  r2   r`  r  hidden_size
vocab_sizer(  ru  ra   s     r'   r1   %Emu3ForConditionalGeneration.__init__  sS     v&
yy!3!3!?!?ASASA^A^ejkr&   c                 6    U R                   R                  5       $ r/   )r  r  r  s    r'   r  1Emu3ForConditionalGeneration.get_input_embeddings#  s    zz..00r&   c                 :    U R                   R                  U5        g r/   )r  r  r  s     r'   r  1Emu3ForConditionalGeneration.set_input_embeddings&  s    

''.r&   rB   c                     U R                   $ r/   )r(  r  s    r'   get_output_embeddings2Emu3ForConditionalGeneration.get_output_embeddings)  s    ||r&   c                 :    U R                   R                  U5        g r/   )r  r  r  s     r'   r  (Emu3ForConditionalGeneration.set_decoder,  s    

w'r&   c                 6    U R                   R                  5       $ r/   )r  r  r  s    r'   r  (Emu3ForConditionalGeneration.get_decoder/  s    zz%%''r&   c                 .    U R                   R                  $ r/   )r  r  r  s    r'   r  'Emu3ForConditionalGeneration.text_model3  s    zz$$$r&   c                 .    U R                   R                  $ r/   )r  r  r  s    r'   r  $Emu3ForConditionalGeneration.vqmodel7  s    zz!!!r&   c                 .    U R                   R                  $ r/   )r  r  r  s    r'   r  /Emu3ForConditionalGeneration.vocabulary_mapping;  s    zz,,,r&   c                 :    U R                   R                  " S0 UD6$ r   )r  r  )r7   rA   s     r'   r  0Emu3ForConditionalGeneration.decode_image_tokens?  s    zz--777r&   r  rA  rw  r;   r<   r  r  r>   r?   labelslogits_to_keeprA   c                    U R                   " SUUUUUUU	S.UD6nUS   n[        U[        5      (       a  [        U* S5      OUnU R	                  USS2USS24   5      nSnU
b3  U R
                  " SUXR                  R                  R                  S.UD6n[        UUUR                  UR                  UR                  S9$ )aL  
image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`):
    The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using
    [`AutoImageProcessor`]. See [`Emu3ImageProcessor.__call__`] for details ([]`Emu3Processor`] uses
    [`Emu3ImageProcessor`] for processing images).
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 Emu3Processor, Emu3ForConditionalGeneration
>>> import torch
>>> import requests
>>> from PIL import Image

>>> model = Emu3ForConditionalGeneration.from_pretrained("BAAI/Emu3-Chat-hf", torch_dtype=torch.bfloat16)
>>> processor = Emu3Processor.from_pretrained("BAAI/Emu3-Chat-hf")

>>> conversation = [
...     {
...     "role": "system",
...     "content": [
...         {"type": "text", "text": "You are a helpful assistant."},
...         ],
...     },
...     {
...     "role": "user",
...     "content": [
...         {"type": "image"},
...         {"type": "text", "text": "Please describe the image."},
...         ],
...     },
... ]

>>> prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
>>> image = Image.open(requests.get("https://www.ilankelman.org/stopsigns/australia.jpg", stream=True).raw)

>>> inputs = processor(images=[image], text=[prompt], return_tensors="pt").to(model.device, torch.bfloat16)

>>> generated_ids = model.generate(**inputs, max_new_tokens=100, do_sample=False)
>>> processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
```)r  r;   r<   r  r  r>   r?   r   N)logitsr@  r+  )lossrC  r  r:   r  r   )r  r[  rL   slicer(  loss_functionr,   r  r+  r	   r  r:   r  )r7   r  rA  rw  r;   r<   r  r  r>   r?   r@  rA  rA   r!  r:   slice_indicesrC  rD  s                     r'   rJ   $Emu3ForConditionalGeneration.forwardB  s    | ** 	
)%+')	
 	
  
8B>SV8W8W~ot4]kmA}a,?@A%% f9P9P9[9[_eD &#33!//))
 	
r&   c	                 V   > [         TU ]  " U4UUUUUUUS.U	D6n
US   S:w  a  S U
S'   U
$ )N)r  r;   r  r?   r<   rA  r>   r   rA  )r0   prepare_inputs_for_generation)r7   r  r  r;   r  r?   r<   r>   rA  rA   model_inputsr8   s              r'   rJ  :Emu3ForConditionalGeneration.prepare_inputs_for_generation  sZ     w<

+)')%%

 

 !!+/L(r&   )r(  r  )NNNNNNNNNNr   )NNNNNTN)'r!   r"   r#   r$   r  _tied_weights_keysr#  r1   r  r  r2   r  r2  r  r  propertyr  r  r  r  r   r   rM   rO   rR   rN   r   r   rP   r   rL   r   r   rQ   r	   rJ   rJ  r%   rS   rT   s   @r'   r&  r&    s   *+/#(&"1/ryy (( % % " " - -8  '+*.$(1537+/59$(59-134X
##X
 ''X
 \\	X

 !.X
 u//0X
 "%X
   1 12X
 D>X
 !!1!12X
 ))*X
 c5<</0X
 +,X
 
u,,	-X
  X
z  r&   r&  )r&  r  r  r  rS  r  )Ir8  	functoolsr   typingr   r   rM   torch.nnr2   torch.nn.functional
functionalr   torch.utils.checkpointcache_utilsr   
generationr   modeling_outputsr	   modeling_utilsr
   processing_utilsr   utilsr   r   r   chameleon.modeling_chameleonr   r   llama.modeling_llamar   r   r   r   r   siglip.modeling_siglipr   configuration_emu3r   r   r   
get_loggerr!   loggerr   r*   r  rV   r   r   r   r   r   r   r   r   r   r   r   r   r  r%  r/  rG  rS  r  r  r  r  r  r&  __all__r   r&   r'   <module>rb     s  "  % "        ) 6 - & > > w v 4 K K 
		H	%	N 	
#( #L$ryy $D	%H 	299 bii :!299 !H		 .")) &.(299 .(b<(299 <(~&o &V V299 D8 8v7ryy 7tCryy CLCryy CL l2 l2l2^3% 3%l'2I '

J 3 

&(;_ 8V# Vrh#6 hVr&   