
    sg&                     l   d Z ddlZddlZddlZddlmZmZmZmZ ddl	Z	ddl
Z	ddl	mZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'  e#jP                  e)      Z*dZ+dZ,d Z- G d dej\                        Z/ G d dej\                        Z0 G d dej\                        Z1 G d dej\                        Z2 G d de      Z3dZ4dZ5 e!de4       G d  d!e3             Z6 e!d"e4       G d# d$e3e             Z7 e!d%e4       G d& d'e3             Z8y)(zPyTorch OpenAI ImageGPT model.    N)AnyOptionalTupleUnion)nn)autocast)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)GenerationMixin))BaseModelOutputWithPastAndCrossAttentions!CausalLMOutputWithCrossAttentions SequenceClassifierOutputWithPast)PreTrainedModel)Conv1D find_pruneable_heads_and_indicesprune_conv1d_layer)add_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstringstorch_float   )ImageGPTConfigzopenai/imagegpt-smallr   c                    	 ddl }ddl}t
        j                  j                  |      }t        j                  dj                  |             |j                  j                  |      }g }g }|D ]v  \  }	}
t        j                  dj                  |	|
             |j                  j                  ||	      }|j                  |	       |j                  |j                                x t        ||      D ]  \  }	}|	dd }	|	j!                  d      }	t#        d |	D              s|	d	   d
v r4t        j                  dj                  dj%                  |	                   j| }|	d	   dvrt'        |d      }|	D ]W  }|j)                  d|      r|j!                  d|      }n|g}|d   dk(  s|d   dk(  rt'        |d      }n|d   dk(  rt'        |d      }n|d   dk(  s|d   dk(  rt'        ||d         }t'        |d      }n|d   dv rt'        |d      }t'        |d      }nt+        |	      dk(  r,|	d   dk(  r$|d   dk(  rt'        ||d         }t'        |d      }nQ|d   dk(  rt'        |d      }t'        |d      }n0|d   dk(  rt'        |d      }t'        |d      }nt'        ||d         }t+        |      d k\  sEt-        |d         }||   }Z t+        |	      dkD  r|	d   dk(  s|	d	   dk(  s|	d	   dk(  s|	d	   dk(  rn	 |j.                  |j.                  k(  sJ 	 t        j                  d!j                  |	             |	d	   d"k(  rbt5        j6                  |j9                  |j:                  |j:                              j<                  |j>                  ddd|j:                  f<   |	d	   d#k(  rot5        j6                  |j9                  |j:                  |j:                              j<                  |j>                  dd|j:                  d |j:                  z  f<   -|	d	   d$k(  ret5        j6                  |j9                  |j:                  |j:                              j<                  |j>                  ddd |j:                  z  df<   t+        |	      dk(  rP|	d   dk(  rH|	d    dk(  r@t5        j6                  |j9                  |j:                  |j:                              |_        |	d	   dk(  rt5        j6                  |      |_        |	d	   dk(  r7t5        j6                  |      |j>                  d|j@                  dz
  ddf<   [|	d	   dk(  r$t5        j6                  |      |j>                  d	<   t5        j6                  |      |_         | S # t        $ r t        j	                  d        w xY w# t0        $ r1}|xj2                  |j.                  |j.                  fz  c_         d}~ww xY w)%z0
    Load tf checkpoints in a pytorch model
    r   NzLoading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.z(Converting TensorFlow checkpoint from {}z"Loading TF weight {} with shape {}   /c              3   $   K   | ]  }|d v  
 yw))adam_vadam_mAdamWeightDecayOptimizerAdamWeightDecayOptimizer_1global_stepN ).0ns     a/var/www/html/venv/lib/python3.12/site-packages/transformers/models/imagegpt/modeling_imagegpt.py	<genexpr>z.load_tf_weights_in_imagegpt.<locals>.<genexpr>V   s      
 nn
   )_stepzSkipping {})wtettransformerz[A-Za-z]+\d+z(\d+)wgweightbbiaswpewte)q_projk_projv_projc_attnr   r   attnc_projr.   lm_headsos   zInitialize PyTorch weight {}r7   r8   r9   )!re
tensorflowImportErrorloggererrorospathabspathinfoformattrainlist_variablesload_variableappendsqueezezipsplitanyjoingetattr	fullmatchlenintshapeAssertionErrorargstorch
from_numpyreshapen_embdTdata
vocab_size)modelconfigimagegpt_checkpoint_pathr@   tftf_path	init_varsnamesarraysnamerW   arraypointerm_namescope_namesnumes                    r)   load_tf_weights_in_imagegptrp   5   s   	 ggoo67G
KK:AA'JK''0IEF  'e8??eLM&&w5Temmo&	' 5&) L3eABxzz#  

 
 "X"KK,,SXXd^<=88#g}5G 	'F||OV4 hhx8%h1~$A#(=!'84Q3&!'62Q5(KNe,C!';q>:!'84Q#AA!'84!'84TaDGv$5+a.H:T!';q>:!'84Q6)!'95!'84Q5(!'51!'84!';q>:;1$+a.)!#,;	'> t9q=T!W.$r(f2DRTYHY]abd]ein]n}}333
 	299$?@8x/4/?/?fmm]c]j]j@k/l/n/nGLLOfmmO+,"X!AFAQAQfmmV]];Ba LLFMMA,===> "X!383C3CEMMRXR_R_agananDo3p3r3rGLLA-//0Y!^Q6 1d1g6I ++EMM&--,WXGL"X ++E2GL"X7<7G7G7NGLL06,,q00!34"X$//6GLL ++E2GLYL3\ LC  Q	
 	P " 7==%++66s#   V ?V< V9<	W6,W11W6c                   T     e Zd Zddee   def fdZdej                  de	fdZ
 xZS )ImageGPTLayerNormhidden_sizeepsc                     t         |           || _        t        j                  t        j                  |            | _        y N)super__init__rt   r   	ParameterrZ   Tensorr2   )selfrs   rt   	__class__s      r)   rx   zImageGPTLayerNorm.__init__   s.    ll5<<#<=    tensorreturnc                     |t        j                  t        j                  t        j                  |      dd      | j                  z         z  | j
                  j                  dd d f   z  S )Nr,   T)axiskeepdim.)rZ   sqrtmeansquarert   r2   r_   )r{   r~   s     r)   forwardzImageGPTLayerNorm.forward   s[     jjELL$8r4PSWS[S[[\]kksAv&'	
r}   )gh㈵>)__name__
__module____qualname__r   rV   floatrx   rZ   rz   tupler   __classcell__r|   s   @r)   rr   rr      s1    >E#J >U >

ell 
u 
r}   rr   c                   "    e Zd Zddee   dee   f fdZd ZddZddZ	d Z
d Z	 	 	 	 	 	 	 dd	ej                  d
ee   deej                     deej                     deej                     deej                     dee   dee   defdZ xZS )ImageGPTAttentionis_cross_attention	layer_idxc           	         t         |           |j                  }| j                  dt	        j
                  t	        j                  ||ft        j                              j                  dd||      d       | j                  dt	        j                  d      d       |j                  | _        |j                  | _        | j                  | j                  z  | _        | j                  | _        | j                  | j                  z  | j                  k7  r&t!        d| j                   d	| j                   d
      |j"                  | _        || _        |j&                  | _        || _        |j*                  | _        | j$                  rNt-        d| j                  z  | j                        | _        t-        | j                  | j                        | _        n(t-        d| j                  z  | j                        | _        t-        | j                  | j                        | _        t5        j6                  |j8                        | _        t5        j6                  |j<                        | _        tA               | _!        y )Nr4   dtyper   F)
persistentmasked_biasg     z=`embed_dim` must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).r?   r   )"rw   rx   max_position_embeddingsregister_bufferrZ   trilonesboolviewr~   rs   	embed_dimnum_attention_heads	num_headshead_dim
split_size
ValueErrorscale_attn_weightsr   scale_attn_by_inverse_layer_idxr   reorder_and_upcast_attnr   r:   q_attnr<   r   Dropout
attn_pdropattn_dropoutresid_pdropresid_dropoutsetpruned_heads)r{   rb   r   r   max_positionsr|   s        r)   rx   zImageGPTAttention.__init__   s   66JJuzz=-"@

STYY1m]  	 	
 	]ELL,>5Q++33$..8..==4>>)T^^;OPTP^P^O_ `NN#2' 
 #)";";"4 06/U/U,"'-'E'E$"" T^^!3T^^DDK @DK T^^!3T^^DDKT^^T^^<JJv'8'89ZZ(:(:;Er}   c                 F   t        |      dk(  ry t        || j                  | j                  | j                        \  }}t        j                  ||| j                  z   |d| j                  z  z   g      }t        | j                  |d      | _	        t        | j                  |d      | _
        | j                  | j                  z  | j                  t        |      z
  z  | _        | j                  t        |      z
  | _        | j                  j                  |      | _        y )Nr   r?   r   dim)rU   r   r   r   r   rZ   catr   r   r:   r<   union)r{   headsindex
index_attns       r)   prune_headszImageGPTAttention.prune_heads   s    u:?7t~~t}}^b^o^opuYYut'>T__I\@]^_
 )jaH(eC  ??dnn<RUV[R\A\]#e*4 --33E:r}   c                 `   t        j                  ||j                  dd            }| j                  r |t	        |j                  d      dz        z  }| j                  r|t        | j                  dz         z  }| j                  s|j                  d      |j                  d      }}| j                  d d d d ||z
  |d |f   }	t        j                  |j                        j                  }
t        j                  |
|j                        j                  |j                         }
t        j"                  |	||
      }|||z   } t%        j&                  d      |      }|j)                  |j                        }| j+                  |      }|||z  }t        j                  ||      }||fS )Nr,         ?r   r   r   )rZ   matmul	transposer   r   sizer   r   r   r   r4   finfor   minr~   todevicewherer   Softmaxtyper   )r{   querykeyvalueattention_mask	head_maskattn_weightsquery_length
key_lengthcausal_mask
mask_valueattn_outputs               r)   _attnzImageGPTAttention._attn   s|   ||E3==R+@A""'+ejjn6K*LLL //'%0B*CCL&&',zz"~sxx|*L))Aq*|*Cj*PR]S]R]$]^K\%7%78<<J j8J8JKNN|ObObcJ ;;{L*ML%'.8L)rzzb),7 $((5((6  ')3Lll<7L((r}   c                 j   |j                         \  }}}}	|j                         \  }
}
}}
t        j                  ||z  ||t        j                  |j                        }d}| j
                  r |t        |j                  d            dz  z  }| j                  r|t        | j                  dz         z  }t        d      5  |j                  d||	      |j                  dd      j                  d|	|      }}t        j                  ||j                         |j                         d	|
      }|j                  ||||      }d d d        | j                  s|j                  d      |j                  d      }}| j                  d d d d ||z
  |d |f   }t        j                  |j                         j"                  }t        j$                  ||j                         j'                  |j                        }t        j(                  |||      }|||z   } t+        j,                  d      |      }|j                   t        j                  k7  rt/        d      |j1                  |j                         }| j3                  |      }|||z  }t        j4                  ||      }||fS # 1 sw Y   hxY w)Nr   r         ?r,   r   r   F)enabledr   r   )betaalphar   r   zDError with upcasting, attn_weights does not have dtype torch.float32)r   rZ   emptyfloat32r   r   r   r   r   r   r\   r   baddbmmr   r4   r   r   r   r~   r   r   r   r   RuntimeErrorr   r   r   )r{   r   r   r   r   r   bszr   	q_seq_lendk_	k_seq_lenr   scale_factorqkr   r   r   r   r   s                        r)   _upcast_and_reordered_attnz,ImageGPTAttention._upcast_and_reordered_attn  si   (-

%Y	2 XXZ1i {{3?IyPUP]P]fkfrfrs ""E%**R.1S88L//E$..1"455L e$ 	V==Y3S]]2r5J5R5RSUWY[d5eqA ==qwwy!'')RS[ghL'//Y	9UL	V
 &&',zz"~sxx|*L))Aq*|*Cj*PR]S]R]$]^K\%7%78<<J j8J8JKNN|ObObcJ ;;{L*ML%'.8L)rzzb),7 .eff#((5((6  ')3Lll<7L((C	V 	Vs   BJ((J2c                 x    |j                         dd ||fz   } |j                  | }|j                  dddd      S )zJ
        Splits hidden_size dim into attn_head_size and num_heads
        Nr,   r   r?   r   r   )r   r   permuter{   r~   r   attn_head_size	new_shapes        r)   _split_headszImageGPTAttention._split_headsE  sE     KKM#2&)^)DD	i(~~aAq))r}   c                     |j                  dddd      j                         }|j                         dd ||z  fz   }|j                  |      S )zS
        Merges attn_head_size dim and num_attn_heads dim into hidden_size
        r   r?   r   r   Nr   )r   
contiguousr   r   r   s        r)   _merge_headszImageGPTAttention._merge_headsM  sO     1a+668KKM#2&)n*D)FF	{{9%%r}   hidden_states
layer_pastr   r   encoder_hidden_statesencoder_attention_mask	use_cacheoutput_attentionsr   c	                    |Zt        | d      st        d      | j                  |      }	| j                  |      j	                  | j
                  d      \  }
}|}n0| j                  |      j	                  | j
                  d      \  }	}
}| j                  |	| j                  | j                        }	| j                  |
| j                  | j                        }
| j                  || j                  | j                        }|7|\  }}t        j                  ||
fd      }
t        j                  ||fd      }|du r|
|f}nd }| j                  r| j                  |	|
|||      \  }}n| j                  |	|
|||      \  }}| j                  || j                  | j                        }| j                  |      }| j!                  |      }||f}|r||fz  }|S )Nr   zIf class is used as cross attention, the weights `q_attn` have to be defined. Please make sure to instantiate class with `ImageGPTAttention(..., is_cross_attention=True)`.r?   r   r   T)hasattrr   r   r:   rP   r   r   r   r   rZ   r   r   r   r   r   r<   r   )r{   r   r   r   r   r   r   r   r   r   r   r   past_key
past_valuepresentr   r   outputss                     r)   r   zImageGPTAttention.forwardU  s    !,4* t 
 KK.E%:;AA$//WXAYJC3N $M : @ @VW @ XE3!!%GT^^T]]C!!%G!#- Hj))XsO4CIIz51r:EElGG''(,(G(GsTY[ikt(u%K(,

5#unV_(`%K''T^^T]]Skk+.((5(&Gr}   )FN)NNNNNNNFF)r   r   r   r   r   rV   rx   r   r   r   r   r   rZ   rz   r   r   r   r   s   @r)   r   r      s    )"8D> )"V^_bVc )"V;$)L2)h*& &*15,08<9=$),13||3 TN3 !.	3
 ELL)3  (53 !) 63 D>3 $D>3 
3r}   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )ImageGPTMLPc                     t         |           |j                  }t        ||      | _        t        ||      | _        t        |j                     | _        t        j                  |j                        | _        y rv   )rw   rx   rs   r   c_fcr<   r   activation_functionactr   r   r   dropout)r{   intermediate_sizerb   r   r|   s       r)   rx   zImageGPTMLP.__init__  s_    &&	,i8	Y(9:&445zz&"4"45r}   r   r   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S rv   )r   r   r<   r   )r{   r   s     r)   r   zImageGPTMLP.forward  s@    		-0/M2]3r}   )r   r   r   rx   rZ   rz   r   r   r   s   @r)   r   r     s#    6U\\ ell r}   r   c                        e Zd Zd fd	Z	 	 	 	 	 	 	 ddej
                  dee   deej
                     deej
                     deej
                     deej
                     dee   d	ee   d
efdZ	 xZ
S )ImageGPTBlockc                    t         |           |j                  }|j                  |j                  nd|z  }t	        ||j
                        | _        t        ||      | _        t	        ||j
                        | _	        |j                  r/t        |d|      | _        t	        ||j
                        | _        t        ||      | _        y )N   rt   r   T)r   r   )rw   rx   rs   n_innerrr   layer_norm_epsilonln_1r   r;   ln_2add_cross_attentioncrossattentionln_cross_attnr   mlp)r{   rb   r   rs   	inner_dimr|   s        r)   rx   zImageGPTBlock.__init__  s    ((&,nn&@FNNa+o	%kv7P7PQ	%f	B	%kv7P7PQ	%%"3Ft_h"iD!2;FD]D]!^Dy&1r}   r   r   r   r   r   r   r   r   r   c	                    |}	| j                  |      }| j                  ||||||      }
|
d   }|
dd  }||	z   }|Wt        | d      st        d|  d      |}	| j	                  |      }| j                  ||||||      }|d   }|	|z   }||dd  z   }|}	| j                  |      }| j                  |      }|	|z   }|f|r|z   }|S |dd  z   }|S )	N)r   r   r   r   r   r   r   r  z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`)r   r   r   r   r   r?   )r  r;   r   r   r  r  r  r  )r{   r   r   r   r   r   r   r   r   residualattn_outputsr   r   cross_attn_outputsfeed_forward_hidden_statess                  r)   r   zImageGPTBlock.forward  sN    !		-0yy!)/ ! 
 #1oqr"#h. ,4!12 =dV DZ Z  %H ..}=M!%!4!4-#&;'="3 "5 " -Q/K${2M 212 66G 		-0%)XXm%<" #== "gL AHLr}   rv   r   )r   r   r   rx   rZ   rz   r   r   r   r   r   r   s   @r)   r  r    s    2$ &*15,08<9=$),18||8 TN8 !.	8
 ELL)8  (58 !) 68 D>8 $D>8 
8r}   r  c                   B     e Zd ZdZeZeZdZdZ	dZ
dgZ fdZd Z xZS )ImageGPTPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    r/   	input_idsTr  c                 $    t        |   |i | y rv   )rw   rx   )r{   inputskwargsr|   s      r)   rx   z ImageGPTPreTrainedModel.__init__  s    &+F+r}   c           	         t        |t        j                  t        f      rl|j                  j
                  j                  d| j                  j                         |j                  |j                  j
                  j                          nt        |t        j                        ry|j                  j
                  j                  d| j                  j                         |j                  g|j                  j
                  |j                     j                          n5t        |t              r%|j                  j
                  j                  d       |j                         D ]m  \  }}d|v sd|v s|j
                  j                  d| j                  j                  t!        j"                  d| j                  j$                  z        z         o y)zInitialize the weights.g        )r   stdNr   r<   r2   r?   )
isinstancer   Linearr   r2   r_   normal_rb   initializer_ranger4   zero_	Embeddingpadding_idxrr   fill_named_parametersmathr   n_layer)r{   moduleri   ps       r)   _init_weightsz%ImageGPTPreTrainedModel._init_weights  sQ   fryy&12 MM&&CT[[5R5R&S{{&  &&(-MM&&CT[[5R5R&S!!-""6#5#56<<> 12MM$$S) ..0 	sGD!4H$4Cdkk.K.KdiiXY\`\g\g\o\oXoNp.pr	sr}   )r   r   r   __doc__r   config_classrp   load_tf_weightsbase_model_prefixmain_input_namesupports_gradient_checkpointing_no_split_modulesrx   r,  r   r   s   @r)   r  r    s9    
 "L1O%!O&*#(),sr}   r  aB  

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

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

    Parameters:
        config ([`ImageGPTConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
            `past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states). Indices of input
            sequence tokens in the vocabulary.

            If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
            `input_ids`.

            Indices can be obtained using [`AutoImageProcessor`]. See [`ImageGPTImageProcessor.__call__`] for details.

        past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`):
            Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see
            `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have
            their past given to this model should not be passed as `input_ids` as they have already been computed.
        attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.

            If `past_key_values` is used, optionally only the last `inputs_embeds` have to be input (see
            `past_key_values`).
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
zbThe bare ImageGPT Model transformer outputting raw hidden-states without any specific head on top.c            #           e Zd Zdef fdZd Zd Zd Z ee	       e
ee      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deeeej                           d	eej                     d
eej                     deej                     deej                     deej                     deej                     deej                     dee   dee   dee   dee   dedeeef   fd              Z xZS )ImageGPTModelrb   c           	      v   t         |   |       |j                  | _        t	        j
                  |j                  | j                        | _        t	        j
                  |j                  | j                        | _	        t	        j                  |j                        | _        t	        j                  t        |j                        D cg c]  }t!        ||       c}      | _        t%        | j                  |j&                        | _        d| _        d | _        d| _        | j1                          y c c}w )Nr  r  F)rw   rx   rs   r   r   r$  r`   r6   r   r5   r   
embd_pdropdrop
ModuleListrangenum_hidden_layersr  hrr   r
  ln_fmodel_parallel
device_mapgradient_checkpointing	post_init)r{   rb   ir|   s      r)   rx   zImageGPTModel.__init__f  s     ++<< 1 14>>B<< > >OJJv001	ERXRjRjLklqf Blm%dnn&:S:ST	 $&+#  ms   
D6c                     | j                   S rv   r6   r{   s    r)   get_input_embeddingsz"ImageGPTModel.get_input_embeddingsy  s    xxr}   c                     || _         y rv   rD  r{   new_embeddingss     r)   set_input_embeddingsz"ImageGPTModel.set_input_embeddings|  s	    !r}   c                     |j                         D ]-  \  }}| j                  |   j                  j                  |       / y)zv
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer}
        N)itemsr<  r;   r   )r{   heads_to_prunelayerr   s       r)   _prune_headszImageGPTModel._prune_heads  s<     +002 	2LE5FF5M**51	2r}   output_typer.  r  past_key_valuesr   token_type_idsposition_idsr   inputs_embedsr   r   r   r   output_hidden_statesreturn_dictr  r   c                   $ d|v r8t        j                  dt               |t        d      |j	                  d      }||n| j
                  j                  }||n| j
                  j                  }|
|
n| j
                  j                  }
||n| j
                  j                  }||t        d      |G| j                  ||       |j                         }|j                  d|d         }|j                  d   }n0|#|j                         dd }|j                  d   }nt        d      ||j                  n|j                  }||j                  d|d         }|%d}t        dgt!        | j"                        z        }n|d   d   j                  d	      }|>t%        j&                  ||d   |z   t$        j(                  |
      }|j+                  d      }|z|dk  rt        d      |j                  |d      }|ddddddf   }|j-                  | j.                        }d|z
  t%        j0                  | j.                        j2                  z  }| j
                  j4                  rE|C|j                         \  }}}||f}|	t%        j6                  ||      }	| j9                  |	      }	nd}	| j;                  || j
                  j<                        }|| j?                  |      }| jA                  |      }||z   $|| j?                  |      }$|z   $| jC                  $      $|$j                  d      fz   }| jD                  r%| jF                  r|
rtH        jK                  d       d}
|
rdnd}|rdnd}|r| j
                  j4                  rdnd}|rdnd}tM        tO        | j"                  |            D ]  \  }\  }} | jP                  rt$        jR                  jU                  $j                         | t        $fd| D              } ||j-                  $j                        }tW        |t$        jX                        r|j-                  $j                        }|r|$fz   }| jD                  r3| jF                  r'| j[                  |j\                  $d|||   ||	|
|	      }!n |$| |||   ||	|
|      }!|!d   $|
du r	||!d   fz   }|r0||!|
rdnd   fz   }| j
                  j4                  r||!|
rdnd   fz   }| jP                  sS| j^                  ja                         D ]J  \  }"}#||#d   k(  sdtc        |"      z   | jd                  k7  s+$j-                  dtc        |"dz         z         $L  | jg                  $      $ $j                  | $|r|$fz   }|st        d $||||fD              S ti        $||||      S )aR  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, ImageGPTModel
        >>> from PIL import Image
        >>> import requests

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> image_processor = AutoImageProcessor.from_pretrained("openai/imagegpt-small")
        >>> model = ImageGPTModel.from_pretrained("openai/imagegpt-small")

        >>> inputs = image_processor(images=image, return_tensors="pt")
        >>> outputs = model(**inputs)
        >>> last_hidden_states = outputs.last_hidden_state
        ```pixel_values`The `pixel_values` argument is deprecated and will be removed in v4.47, use `input_ids` instead.N_You cannot pass both `pixel_values` and `input_ids`. Please make sure to only pass `input_ids`.zDYou cannot specify both input_ids and inputs_embeds at the same timer,   r   z5You have to specify either input_ids or inputs_embedsr   r   z$batch_size has to be defined and > 0r   r   )r   zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fr&   c              3   T   K   | ]  }|j                  j                         ! y wrv   )r   r   )r'   
past_stater   s     r)   r*   z(ImageGPTModel.forward.<locals>.<genexpr>%  s      &hzz}}]5I5I'J&hs   %()r   r   r   r   r   r   r   Tr   r?   r   zcuda:c              3   $   K   | ]  }|| 
 y wrv   r&   )r'   vs     r)   r*   z(ImageGPTModel.forward.<locals>.<genexpr>]  s      = r+   )last_hidden_staterR  r   
attentionscross_attentions)5warningswarnFutureWarningr   poprb   r   rV  r   use_return_dict%warn_if_padding_and_no_attention_maskr   r   rW   r   r   rU   r<  rZ   arangelong	unsqueezer   r   r   r   r  r   invert_attention_maskget_head_maskr)  r6   r5   r8  r@  trainingrC   warning_once	enumeraterO   r>  cuda
set_devicer  rz   _gradient_checkpointing_func__call__r?  rL  strlast_devicer=  r   )%r{   r  rR  r   rS  rT  r   rU  r   r   r   r   rV  rW  r  input_shape
batch_sizer   past_lengthencoder_batch_sizeencoder_sequence_lengthr   encoder_hidden_shapeposition_embedstoken_type_embedsoutput_shapepresentsall_self_attentionsall_cross_attentionsall_hidden_statesrB  blockr   r   r   r_  r   s%                                       @r)   r   zImageGPTModel.forward  s'   Z V#MMr
 $ u  

>2I1B1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B] ]%>cdd"66y.Q#..*K!r;r?;I"+J&',,.s3K&,,Q/JTUU%.%:!!@T@T%+00[_EN"K#TFS[$89O)!,Q/44R8K <<[_{5RZ_ZdZdmstL'11!4L %Q !GHH+00R@N ,AtT1,<=N ,..TZZ.@N!N2ekk$**6M6Q6QQN ;;**/D/P=R=W=W=Y: 7$68O#P %-).4HQW)X&%)%?%?@V%W"%)" &&y$++2E2EF	  HHY/M((<0%7% $ 8),==M		-0"m&8&8&<%>>&&4==##p "	"2$5b4%64;;;Z;Zr`d"6BD&/DFFO0L&M 4	O"A"z""

%%m&:&:;)!&&h]g&h!hJ!-%3%6%6}7K7K%LNi6 )]-A-A BI#$58H$H!**t}};;NN!"aL)*%
  !)#1'l*?+A'&7	 $AJMD #wqzm3 &9W)QYZ=[<]&]#;;22+?7PY1_`CaBc+c( "" OO113 ODAqAbEzgA&6$:J:J&J(5(8(83q1u:9M(NOe4	Ol 		-0***L9 1]4D D '3DFY[op   9+$+*1
 	
r}   )NNNNNNNNNNNNN)r   r   r   r   rx   rF  rJ  rO  r   IMAGEGPT_INPUTS_DOCSTRINGr   r   _CONFIG_FOR_DOCr   rZ   rz   r   r   r   r   r   r   r   s   @r)   r5  r5  a  s   
~ &"2 ++DE+Tcrs -1@D1515/3,0048<9=$(,0/3&*a
ELL)a
 "%ell(;"<=a
 !.	a

 !.a
 u||,a
 ELL)a
  -a
  (5a
 !) 6a
 D>a
 $D>a
 'tna
 d^a
 a
  
u??	@!a
 t Fa
r}   r5  z
    The ImageGPT Model transformer with a language modeling head on top (linear layer with weights tied to the input
    embeddings).
    c            %       p    e Zd ZdgZdef fdZd Zd Z ee	       e
ee      	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deeeej                           d	eej                     d
eej                     deej                     deej                     deej                     deej                     deej                     deej                     dee   dee   dee   dee   dedeeef   f d              Zedeeej                        dej                  deeej                        fd       Z xZS )ImageGPTForCausalImageModelingzlm_head.weightrb   c                     t         |   |       t        |      | _        t	        j
                  |j                  |j                  dz
  d      | _        d| _	        d | _
        | j                          y )Nr   Fr4   )rw   rx   r5  r/   r   r   r]   r`   r=   r>  r?  rA  r{   rb   r|   s     r)   rx   z'ImageGPTForCausalImageModeling.__init__v  s[     (0yy0A0AA0EER $r}   c                     | j                   S rv   r=   rE  s    r)   get_output_embeddingsz4ImageGPTForCausalImageModeling.get_output_embeddings  s    ||r}   c                     || _         y rv   r  rH  s     r)   set_output_embeddingsz4ImageGPTForCausalImageModeling.set_output_embeddings  s	    %r}   rP  r  rR  r   rS  rT  r   rU  r   r   labelsr   r   rV  rW  r  r   c                    d|v r8t        j                  dt               |t        d      |j	                  d      }||n| j
                  j                  }| j                  |||||||||	||||      }|d   }| j                  |      }d}|
r|dddddf   j                         }|
dd	df   j                         }t               } ||j                  d|j                  d            |j                  d            }|s|f|d	d z   }||f|z   S |S t        |||j                  |j                  |j                   |j"                  
      S )a  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, ImageGPTForCausalImageModeling
        >>> import torch
        >>> import matplotlib.pyplot as plt
        >>> import numpy as np

        >>> image_processor = AutoImageProcessor.from_pretrained("openai/imagegpt-small")
        >>> model = ImageGPTForCausalImageModeling.from_pretrained("openai/imagegpt-small")
        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        >>> model.to(device)  # doctest: +IGNORE_RESULT

        >>> # unconditional generation of 8 images
        >>> batch_size = 4
        >>> context = torch.full((batch_size, 1), model.config.vocab_size - 1)  # initialize with SOS token
        >>> context = context.to(device)
        >>> output = model.generate(
        ...     input_ids=context, max_length=model.config.n_positions + 1, temperature=1.0, do_sample=True, top_k=40
        ... )

        >>> clusters = image_processor.clusters
        >>> height = image_processor.size["height"]
        >>> width = image_processor.size["width"]

        >>> samples = output[:, 1:].cpu().detach().numpy()
        >>> samples_img = [
        ...     np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [height, width, 3]).astype(np.uint8) for s in samples
        ... ]  # convert color cluster tokens back to pixels
        >>> f, axes = plt.subplots(1, batch_size, dpi=300)

        >>> for img, ax in zip(samples_img, axes):  # doctest: +IGNORE_RESULT
        ...     ax.axis("off")
        ...     ax.imshow(img)
        ```rY  rZ  Nr[  )rR  r   rS  rT  r   rU  r   r   r   r   rV  rW  r   .r,   r   )losslogitsrR  r   ra  rb  )rc  rd  re  r   rf  rb   rg  r/   r=   r   r
   r   r   r   rR  r   ra  rb  )r{   r  rR  r   rS  rT  r   rU  r   r   r  r   r   rV  rW  r  transformer_outputsr   	lm_logitsr  shift_logitsshift_labelsloss_fctoutputs                           r)   r   z&ImageGPTForCausalImageModeling.forward  s   @ V#MMr
 $ u  

>2I%0%<k$++B]B]"..+))%'"7#9/!5# / 
 ,A.LL/	$S#2#q[1<<>L!#qr'?557L')HL--b,2C2CB2GH,J[J[\^J_`D\$7$;;F)-)9TGf$EvE0/??-;;*550AA
 	
r}   beam_idxc                 ,    t        fd| D              S )a  
        This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
        [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
        beam_idx at every generation step.
        c              3   F   K   | ]  }t        fd |D                yw)c              3   t   K   | ]/  }|j                  d j                  |j                               1 yw)r   N)index_selectr   r   )r'   r]  r  s     r)   r*   zJImageGPTForCausalImageModeling._reorder_cache.<locals>.<genexpr>.<genexpr>	  s.     jQ[*))!X[[9J9J-KLjs   58Nr   )r'   r   r  s     r)   r*   z@ImageGPTForCausalImageModeling._reorder_cache.<locals>.<genexpr>  s%      
 j_ijj
s   !r  )rR  r  s    `r)   _reorder_cachez-ImageGPTForCausalImageModeling._reorder_cache  s      
-
 
 	
r}   )NNNNNNNNNNNNNN)r   r   r   _tied_weights_keysr   rx   r  r  r   r  r   r   r  r   rZ   rz   r   r   r   r   r   staticmethodr  r   r   s   @r)   r  r  l  s    ++	~ 	& ++DE+L[jk -1@D1515/3,0048<9=)-$(,0/3&*t
ELL)t
 "%ell(;"<=t
 !.	t

 !.t
 u||,t
 ELL)t
  -t
  (5t
 !) 6t
 &t
 D>t
 $D>t
 'tnt
 d^t
  !t
" 
u77	8#t
 l Ft
l 
uU\\23
?D||
	uU\\"	#
 
r}   r  z
    The ImageGPT Model transformer with an image classification head on top (linear layer).
    [`ImageGPTForImageClassification`] average-pools the hidden states in order to do the classification.
    c            !           e Zd Zdef fdZ ee       eee	      	 	 	 	 	 	 	 	 	 	 	 	 dde
ej                     de
eeej                           de
ej                     de
ej                     de
ej                     d	e
ej                     d
e
ej                     de
ej                     de
e   de
e   de
e   de
e   dedeeef   fd              Z xZS )ImageGPTForImageClassificationrb   c                     t         |   |       |j                  | _        t        |      | _        t        j                  |j                  | j                  d      | _        | j                          y )NFr  )
rw   rx   
num_labelsr5  r/   r   r   r]   scorerA  r  s     r)   rx   z'ImageGPTForImageClassification.__init__  sR      ++(0YYv}}dooEJ
 	r}   rP  r  rR  r   rS  rT  r   rU  r  r   r   rV  rW  r  r   c                    d|v r8t        j                  dt               |t        d      |j	                  d      }||n| j
                  j                  }| j                  ||||||||	|
||      }|d   }|j                  d      }| j                  |      }d}|| j
                  j                  | j                  dk(  rd	| j
                  _
        nl| j                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd
| j
                  _
        nd| j
                  _
        | j
                  j                  d	k(  rIt!               }| j                  dk(  r& ||j#                         |j#                               }n |||      }n| j
                  j                  d
k(  r=t%               } ||j'                  d| j                        |j'                  d            }n,| j
                  j                  dk(  rt)               } |||      }|s|f|dd z   }||f|z   S |S t+        |||j,                  |j.                  |j0                        S )a7  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, ImageGPTForImageClassification
        >>> from PIL import Image
        >>> import requests

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> image_processor = AutoImageProcessor.from_pretrained("openai/imagegpt-small")
        >>> model = ImageGPTForImageClassification.from_pretrained("openai/imagegpt-small")

        >>> inputs = image_processor(images=image, return_tensors="pt")
        >>> outputs = model(**inputs)
        >>> logits = outputs.logits
        ```rY  rZ  Nr[  )
rR  r   rS  rT  r   rU  r   r   rV  rW  r   r   r   
regressionsingle_label_classificationmulti_label_classificationr,   )r  r  rR  r   ra  )rc  rd  re  r   rf  rb   rg  r/   r   r  problem_typer  r   rZ   rj  rV   r   rN   r
   r   r	   r   rR  r   ra  )r{   r  rR  r   rS  rT  r   rU  r  r   r   rV  rW  r  r  r   pooled_hidden_statesr  r  r  r  s                        r)   r   z&ImageGPTForImageClassification.forward  sG   X V#MMr
 $ u  

>2I%0%<k$++B]B]"..+))%'/!5# / 
 ,A.,11a1801{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./Y!4QR!88F)-)9TGf$EvE//??-;;*55
 	
r}   )NNNNNNNNNNNN)r   r   r   r   rx   r   r  r   r   r  r   rZ   rz   r   r   r   r   r   r   r   s   @r)   r  r    si   ~  ++DE+KZij -1@D1515/3,004)-$(,0/3&*l
ELL)l
 "%ell(;"<=l
 !.	l

 !.l
 u||,l
 ELL)l
  -l
 &l
 D>l
 $D>l
 'tnl
 d^l
 l
 
u66	7l
 k Fl
r}   r  )9r-  r(  rE   rc  typingr   r   r   r   rZ   torch.utils.checkpointr   torch.cuda.ampr   torch.nnr	   r
   r   activationsr   
generationr   modeling_outputsr   r   r   modeling_utilsr   pytorch_utilsr   r   r   utilsr   r   r   r   r   configuration_imagegptr   
get_loggerr   rC   _CHECKPOINT_FOR_DOCr  rp   Modulerr   r   r   r  r  IMAGEGPT_START_DOCSTRINGr  r5  r  r  r&   r}   r)   <module>r     sk   %  	  . .    # A A ! ) 
 . Y Y  3 
		H	%- "iX
		 
X		 Xv")) "HBII HV(so (sV  < ~ hD
+ D
	D
N  X
%<o X
X
v  x
%< x
x
r}   