




from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin


class       __lowercase      (	lowercase_			,      lowercase_			,      lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				[r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias''']




     @register_to_config
     def __init__(				self       :			Optional[int] ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			str ,	UpperCamelCase_       :			Tuple = None ,	UpperCamelCase_       :			int = 50_257 ,	UpperCamelCase_       :			Optional[int] = 1_024 ,	UpperCamelCase_       :			List[str] = 768 ,	UpperCamelCase_       :			Any = 12 ,	UpperCamelCase_       :			int = 12 ,	UpperCamelCase_       :			Optional[Any] = None ,	UpperCamelCase_       :			Optional[Any] = "gelu_new" ,	UpperCamelCase_       :			Union[str, Any] = 0.1 ,	UpperCamelCase_       :			Optional[int] = 0.1 ,	UpperCamelCase_       :			Dict = 0.1 ,	UpperCamelCase_       :			Tuple = 1e-5 ,	UpperCamelCase_       :			List[Any] = 0.02 ,	UpperCamelCase_       :			str = True ,	UpperCamelCase_       :			Optional[Any] = True ,	UpperCamelCase_       :			Optional[Any] = False ,	UpperCamelCase_       :			Optional[int] = False ,	):


         """simple docstring"""



         super().__init__()

         __A         =	prefix_length

         if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
             raise ValueError(
                 F"`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and"
                 F" `n_embd`: {n_embd} are not equal."				)

         __A         =	prefix_inner_dim
         __A         =	prefix_hidden_dim

         __A         =	(
             nn.Linear(self.prefix_inner_dim ,	self.prefix_hidden_dim				)
             if self.prefix_hidden_dim is not None
             else nn.Identity()
         )
         __A         =	(
             nn.Linear(self.prefix_hidden_dim ,	A__				) if self.prefix_hidden_dim is not None else nn.Identity()
         )

         __A         =	GPTaConfig(
             vocab_size=A__ ,	n_positions=A__ ,	n_embd=A__ ,	n_layer=A__ ,	n_head=A__ ,	n_inner=A__ ,	activation_function=A__ ,	resid_pdrop=A__ ,	embd_pdrop=A__ ,	attn_pdrop=A__ ,	layer_norm_epsilon=A__ ,	initializer_range=A__ ,	scale_attn_weights=A__ ,	use_cache=A__ ,	scale_attn_by_inverse_layer_idx=A__ ,	reorder_and_upcast_attn=A__ ,	)
         __A         =	GPTaLMHeadModel(A__				)




     def 		lowerCAmelCase_     (				self       :			Optional[Any] ,	UpperCamelCase_       :			List[str] ,	UpperCamelCase_       :			List[str] ,	UpperCamelCase_       :			int = None ,	UpperCamelCase_       :			List[str] = None ,	):


         """simple docstring"""



         __A         =	self.transformer.transformer.wte(A__				)
         __A         =	self.encode_prefix(A__				)
         __A         =	self.decode_prefix(A__				)
         __A         =	torch.cat((prefix_embeds, embedding_text) ,	dim=1				)

         if labels is not None:
             __A         =	self.get_dummy_token(input_ids.shape[0] ,	input_ids.device				)
             __A         =	torch.cat((dummy_token, input_ids) ,	dim=1				)
         __A         =	self.transformer(inputs_embeds=A__ ,	labels=A__ ,	attention_mask=A__				)
         if self.prefix_hidden_dim is not None:
             return out, hidden
         else:
             return out




     def 		lowerCAmelCase_     (				self       :			Tuple ,	UpperCamelCase_       :			Optional[int] ,	UpperCamelCase_       :			Dict				):


         """simple docstring"""



         return torch.zeros(A__ ,	self.prefix_length ,	dtype=torch.intaa ,	device=A__				)




     def 		lowerCAmelCase_     (				self       :			List[str] ,	UpperCamelCase_       :			Optional[int]				):


         """simple docstring"""



         return self.encode_prefix(A__				)




     @torch.no_grad()
     def 		lowerCAmelCase_     (				self       :			Optional[Any] ,	UpperCamelCase_       :			int ,	UpperCamelCase_       :			Optional[Any] ,	UpperCamelCase_       :			List[Any]				):


         """simple docstring"""



         __A         =	torch.split(A__ ,	1 ,	dim=0				)
         __A         =	[]
         __A         =	[]
         for feature in features:
             __A         =	self.decode_prefix(feature.to(A__				)				)  # back to the clip feature
             # Only support beam search for now
             __A         =	self.generate_beam(
                 input_embeds=A__ ,	device=A__ ,	eos_token_id=A__				)
             generated_tokens.append(output_tokens[0]				)
             generated_seq_lengths.append(seq_lengths[0]				)
         __A         =	torch.stack(A__				)
         __A         =	torch.stack(A__				)
         return generated_tokens, generated_seq_lengths





     @torch.no_grad()
     def 		lowerCAmelCase_     (				self       :			str ,	UpperCamelCase_       :			List[Any]=None ,	UpperCamelCase_       :			Any=None ,	UpperCamelCase_       :			str=None ,	UpperCamelCase_       :			str = 5 ,	UpperCamelCase_       :			str = 67 ,	UpperCamelCase_       :			Optional[Any] = 1.0 ,	UpperCamelCase_       :			Dict = None ,	):


         """simple docstring"""



         __A         =	eos_token_id
         __A         =	None
         __A         =	None
         __A         =	torch.ones(A__ ,	device=A__ ,	dtype=torch.int				)
         __A         =	torch.zeros(A__ ,	device=A__ ,	dtype=torch.bool				)

         if input_embeds is not None:
             __A         =	input_embeds
         else:
             __A         =	self.transformer.transformer.wte(A__				)

         for i in range(A__				):
             __A         =	self.transformer(inputs_embeds=A__				)
             __A         =	outputs.logits
             __A         =	logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
             __A         =	logits.softmax(-1				).log()

             if scores is None:
                 __A         =	logits.topk(A__ ,	-1				)
                 __A         =	generated.expand(A__ ,	*generated.shape[1:]				)
                 __A         =	next_tokens.permute(1 ,	0				), scores.squeeze(0				)
                 if tokens is None:
                     __A         =	next_tokens
                 else:
                     __A         =	tokens.expand(A__ ,	*tokens.shape[1:]				)
                     __A         =	torch.cat((tokens, next_tokens) ,	dim=1				)
             else:
                 __A         =	-float(np.inf				)
                 __A         =	0
                 __A         =	scores[:, None] + logits
                 seq_lengths[~is_stopped] += 1
                 __A         =	scores_sum / seq_lengths[:, None]
                 __A         =	scores_sum_average.view(-1				).topk(A__ ,	-1				)
                 __A         =	next_tokens // scores_sum.shape[1]
                 __A         =	seq_lengths[next_tokens_source]
                 __A         =	next_tokens % scores_sum.shape[1]
                 __A         =	next_tokens.unsqueeze(1				)
                 __A         =	tokens[next_tokens_source]
                 __A         =	torch.cat((tokens, next_tokens) ,	dim=1				)
                 __A         =	generated[next_tokens_source]
                 __A         =	scores_sum_average * seq_lengths
                 __A         =	is_stopped[next_tokens_source]

             __A         =	self.transformer.transformer.wte(next_tokens.squeeze()				).view(generated.shape[0] ,	1 ,	-1				)
             __A         =	torch.cat((generated, next_token_embed) ,	dim=1				)
             __A         =	is_stopped + next_tokens.eq(A__				).squeeze()
             if is_stopped.all():
                 break

         __A         =	scores / seq_lengths
         __A         =	scores.argsort(descending=A__				)
         # tokens tensors are already padded to max_seq_length
         __A         =	[tokens[i] for i in order]
         __A         =	torch.stack(A__ ,	dim=0				)
         __A         =	torch.tensor([seq_lengths[i] for i in order] ,	dtype=seq_lengths.dtype				)
         return output_texts, seq_lengths












from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				["image_processor", "tokenizer"]
     SCREAMING_SNAKE_CASE   						=				"AutoImageProcessor"
     SCREAMING_SNAKE_CASE   						=				"AutoTokenizer"




     def __init__(				self       :			str ,	UpperCamelCase_       :			Any ,	UpperCamelCase_       :			str				):


         """simple docstring"""



         super().__init__(UpperCamelCase_ ,	UpperCamelCase_				)
         __A         =	self.image_processor




     def __call__(				self       :			Optional[Any] ,	UpperCamelCase_       :			Optional[Any]=None ,	UpperCamelCase_       :			Any=None ,	UpperCamelCase_       :			List[Any]=None ,	**UpperCamelCase_       :			int				):


         """simple docstring"""



         if text is None and images is None:
             raise ValueError("""You have to specify either text or images. Both cannot be none."""				)

         if text is not None:
             __A         =	self.tokenizer(UpperCamelCase_ ,	return_tensors=UpperCamelCase_ ,	**UpperCamelCase_				)

         if images is not None:
             __A         =	self.image_processor(UpperCamelCase_ ,	return_tensors=UpperCamelCase_ ,	**UpperCamelCase_				)

         if text is not None and images is not None:
             __A         =	image_features.pixel_values
             return encoding
         elif text is not None:
             return encoding
         else:
             return BatchEncoding(data=dict(**UpperCamelCase_				) ,	tensor_type=UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			List[str] ,	*UpperCamelCase_       :			Tuple ,	**UpperCamelCase_       :			List[str]				):


         """simple docstring"""



         return self.tokenizer.batch_decode(*UpperCamelCase_ ,	**UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Any ,	*UpperCamelCase_       :			int ,	**UpperCamelCase_       :			Tuple				):


         """simple docstring"""



         return self.tokenizer.decode(*UpperCamelCase_ ,	**UpperCamelCase_				)





     @property
     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         return ["input_ids", "attention_mask", "pixel_values"]




