
'''simple docstring'''
import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel







class  __A				:
 def __init__(   self			:    Union[str, Any]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    List[Any]=13				,   lowerCamelCase			:    Any=7				,   lowerCamelCase			:    Union[str, Any]=True				,   lowerCamelCase			:    Any=True				,   lowerCamelCase			:    Any=False				,   lowerCamelCase			:    Optional[Any]=True				,   lowerCamelCase			:    str=99				,   lowerCamelCase			:    Dict=32				,   lowerCamelCase			:    Dict=5				,   lowerCamelCase			:    Any=4				,   lowerCamelCase			:    List[Any]=37				,   lowerCamelCase			:    Tuple="gelu"				,   lowerCamelCase			:    List[str]=0.1				,   lowerCamelCase			:    List[str]=0.1				,   lowerCamelCase			:    Any=5_12				,   lowerCamelCase			:    Union[str, Any]=16				,   lowerCamelCase			:    List[str]=2				,   lowerCamelCase			:    List[str]=0.02				,   lowerCamelCase			:    Union[str, Any]=3				,   lowerCamelCase			:    Any=4				,   lowerCamelCase			:    Tuple=None				,   ):



   """simple docstring"""





   __A		:     int												=		parent
   __A		:     Optional[int]												=		batch_size
   __A		:     int												=		seq_length
   __A		:     Optional[int]												=		is_training
   __A		:     List[Any]												=		use_input_mask
   __A		:     Tuple												=		use_token_type_ids
   __A		:     Optional[Any]												=		use_labels
   __A		:     List[str]												=		vocab_size
   __A		:     str												=		hidden_size
   __A		:     Optional[Any]												=		num_hidden_layers
   __A		:     str												=		num_attention_heads
   __A		:     Dict												=		intermediate_size
   __A		:     Any												=		hidden_act
   __A		:     Optional[Any]												=		hidden_dropout_prob
   __A		:     Any												=		attention_probs_dropout_prob
   __A		:     int												=		max_position_embeddings
   __A		:     Dict												=		type_vocab_size
   __A		:     Optional[Any]												=		type_sequence_label_size
   __A		:     Dict												=		initializer_range
   __A		:     Union[str, Any]												=		num_labels
   __A		:     Optional[Any]												=		num_choices
   __A		:     Optional[int]												=		scope
 def 		lowercase_(   self			:    Optional[Any]       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size       )

   __A		:     Optional[int]												=		None
   if self.use_input_mask:
     __A		:     Optional[Any]												=		random_attention_mask([self.batch_size, self.seq_length]       )

   __A		:     Dict												=		None
   if self.use_token_type_ids:
     __A		:     Optional[int]												=		ids_tensor([self.batch_size, self.seq_length]				,   self.type_vocab_size       )

   __A		:     Optional[int]												=		None
   __A		:     int												=		None
   __A		:     Any												=		None
   if self.use_labels:
     __A		:     Optional[Any]												=		ids_tensor([self.batch_size]				,   self.type_sequence_label_size       )
     __A		:     List[str]												=		ids_tensor([self.batch_size, self.seq_length]				,   self.num_labels       )
     __A		:     List[Any]												=		ids_tensor([self.batch_size]				,   self.num_choices       )

   __A		:     Optional[Any]												=		self.get_config()

   return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 		lowercase_(   self			:    Optional[Any]       ):



   """simple docstring"""





   return OpenLlamaConfig(
       vocab_size=self.vocab_size				,   hidden_size=self.hidden_size				,   num_hidden_layers=self.num_hidden_layers				,   num_attention_heads=self.num_attention_heads				,   intermediate_size=self.intermediate_size				,   hidden_act=self.hidden_act				,   hidden_dropout_prob=self.hidden_dropout_prob				,   attention_probs_dropout_prob=self.attention_probs_dropout_prob				,   max_position_embeddings=self.max_position_embeddings				,   type_vocab_size=self.type_vocab_size				,   is_decoder=lowerCamelCase				,   initializer_range=self.initializer_range				,   use_stable_embedding=lowerCamelCase				,   )
 def 		lowercase_(   self			:    Optional[Any]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Optional[Any]       ):



   """simple docstring"""





   __A		:     Optional[Any]												=		OpenLlamaModel(config=lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     str												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase       )
   __A		:     List[str]												=		model(lowerCamelCase       )
   self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)       )
 def 		lowercase_(   self			:    List[Any]				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    int				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Union[str, Any]				,   ):



   """simple docstring"""





   __A		:     List[Any]												=		True
   __A		:     int												=		OpenLlamaModel(lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     Optional[int]												=		model(
       lowerCamelCase				,   attention_mask=lowerCamelCase				,   encoder_hidden_states=lowerCamelCase				,   encoder_attention_mask=lowerCamelCase				,   )
   __A		:     List[str]												=		model(
       lowerCamelCase				,   attention_mask=lowerCamelCase				,   encoder_hidden_states=lowerCamelCase				,   )
   __A		:     List[str]												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase       )
   self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)       )
 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Optional[int]				,   ):



   """simple docstring"""





   __A		:     Dict												=		OpenLlamaForCausalLM(config=lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     List[Any]												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase				,   labels=lowerCamelCase       )
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)       )
 def 		lowercase_(   self			:    List[Any]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    str				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Optional[Any]				,   ):



   """simple docstring"""





   __A		:     Optional[Any]												=		True
   __A		:     str												=		True
   __A		:     int												=		OpenLlamaForCausalLM(config=lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()

   # first forward pass
   __A		:     Optional[Any]												=		model(
       lowerCamelCase				,   attention_mask=lowerCamelCase				,   encoder_hidden_states=lowerCamelCase				,   encoder_attention_mask=lowerCamelCase				,   use_cache=lowerCamelCase				,   )
   __A		:     Union[str, Any]												=		outputs.past_key_values

   # create hypothetical multiple next token and extent to next_input_ids
   __A		:     str												=		ids_tensor((self.batch_size, 3)				,   config.vocab_size       )
   __A		:     Optional[int]												=		ids_tensor((self.batch_size, 3)				,   vocab_size=2       )

   # append to next input_ids and
   __A		:     List[Any]												=		torch.cat([input_ids, next_tokens]				,   dim=-1       )
   __A		:     Dict												=		torch.cat([input_mask, next_mask]				,   dim=-1       )

   __A		:     int												=		model(
       lowerCamelCase				,   attention_mask=lowerCamelCase				,   encoder_hidden_states=lowerCamelCase				,   encoder_attention_mask=lowerCamelCase				,   output_hidden_states=lowerCamelCase				,   )["""hidden_states"""][0]
   __A		:     Any												=		model(
       lowerCamelCase				,   attention_mask=lowerCamelCase				,   encoder_hidden_states=lowerCamelCase				,   encoder_attention_mask=lowerCamelCase				,   past_key_values=lowerCamelCase				,   output_hidden_states=lowerCamelCase				,   )["""hidden_states"""][0]

   # select random slice
   __A		:     int												=		ids_tensor((1,)				,   output_from_past.shape[-1]       ).item()
   __A		:     Tuple												=		output_from_no_past[:, -3:, random_slice_idx].detach()
   __A		:     Optional[Any]												=		output_from_past[:, :, random_slice_idx].detach()

   self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]       )

   # test that outputs are equal for slice
   self.parent.assertTrue(torch.allclose(lowerCamelCase				,   lowerCamelCase				,   atol=1e-3       )       )






 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   __A		:     str												=		self.prepare_config_and_inputs()
   (
       (
       __A
   )							,					(
       __A
   )							,					(
       __A
   )							,					(
       __A
   )							,					(
       __A
   )							,					(
       __A
   )							,					(
       __A
   )							,					
   )		:     Any												=		config_and_inputs
   __A		:     Union[str, Any]												=		{"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_torch
class  __A				( _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						unittest.TestCase			):
 lowerCamelCase				       =(
     (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
 )
 lowerCamelCase				       =(OpenLlamaForCausalLM,) if is_torch_available() else ()
 lowerCamelCase				       =(
     {
         '''feature-extraction''': OpenLlamaModel,
         '''text-classification''': OpenLlamaForSequenceClassification,
         '''text-generation''': OpenLlamaForCausalLM,
         '''zero-shot''': OpenLlamaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowerCamelCase				       =False
 lowerCamelCase				       =False
 def 		lowercase_(   self			:    int       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		OpenLlamaModelTester(self       )
   __A		:     List[str]												=		ConfigTester(self				,   config_class=lowerCamelCase				,   hidden_size=37       )
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   self.config_tester.run_common_tests()
 def 		lowercase_(   self			:    List[Any]       ):



   """simple docstring"""





   __A		:     List[str]												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowerCamelCase       )
 def 		lowercase_(   self			:    Any       ):



   """simple docstring"""





   __A		:     List[Any]												=		self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
     __A		:     Any												=		type
     self.model_tester.create_and_check_model(*lowerCamelCase       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A							,					__A		:     List[str]												=		self.model_tester.prepare_config_and_inputs_for_common()
   __A		:     str												=		3
   __A		:     Dict												=		input_dict["""input_ids"""]
   __A		:     Optional[int]												=		input_ids.ne(1       ).to(lowerCamelCase       )
   __A		:     Optional[Any]												=		ids_tensor([self.model_tester.batch_size]				,   self.model_tester.type_sequence_label_size       )
   __A		:     Optional[Any]												=		OpenLlamaForSequenceClassification(lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     Dict												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase				,   labels=lowerCamelCase       )
   self.assertEqual(result.logits.shape				,   (self.model_tester.batch_size, self.model_tester.num_labels)       )
 def 		lowercase_(   self			:    Any       ):



   """simple docstring"""





   __A							,					__A		:     Union[str, Any]												=		self.model_tester.prepare_config_and_inputs_for_common()
   __A		:     Dict												=		3
   __A		:     Tuple												=		"""single_label_classification"""
   __A		:     Any												=		input_dict["""input_ids"""]
   __A		:     Optional[Any]												=		input_ids.ne(1       ).to(lowerCamelCase       )
   __A		:     List[Any]												=		ids_tensor([self.model_tester.batch_size]				,   self.model_tester.type_sequence_label_size       )
   __A		:     Tuple												=		OpenLlamaForSequenceClassification(lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     Union[str, Any]												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase				,   labels=lowerCamelCase       )
   self.assertEqual(result.logits.shape				,   (self.model_tester.batch_size, self.model_tester.num_labels)       )
 def 		lowercase_(   self			:    int       ):



   """simple docstring"""





   __A							,					__A		:     Any												=		self.model_tester.prepare_config_and_inputs_for_common()
   __A		:     Any												=		3
   __A		:     str												=		"""multi_label_classification"""
   __A		:     Any												=		input_dict["""input_ids"""]
   __A		:     Union[str, Any]												=		input_ids.ne(1       ).to(lowerCamelCase       )
   __A		:     List[str]												=		ids_tensor(
       [self.model_tester.batch_size, config.num_labels]				,   self.model_tester.type_sequence_label_size       ).to(torch.float       )
   __A		:     Dict												=		OpenLlamaForSequenceClassification(lowerCamelCase       )
   model.to(lowerCamelCase       )
   model.eval()
   __A		:     Dict												=		model(lowerCamelCase				,   attention_mask=lowerCamelCase				,   labels=lowerCamelCase       )
   self.assertEqual(result.logits.shape				,   (self.model_tester.batch_size, self.model_tester.num_labels)       )
 @unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test"""       )
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   pass






 @parameterized.expand([("""linear""",), ("""dynamic""",)]       )
 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    str       ):



   """simple docstring"""





   __A							,					__A		:     List[str]												=		self.model_tester.prepare_config_and_inputs_for_common()
   __A		:     Any												=		ids_tensor([1, 10]				,   config.vocab_size       )
   __A		:     Optional[Any]												=		ids_tensor([1, int(config.max_position_embeddings * 1.5       )]				,   config.vocab_size       )

   set_seed(42       )  # Fixed seed at init time so the two models get the same random weights
   __A		:     List[Any]												=		OpenLlamaModel(lowerCamelCase       )
   original_model.to(lowerCamelCase       )
   original_model.eval()
   __A		:     int												=		original_model(lowerCamelCase       ).last_hidden_state
   __A		:     List[Any]												=		original_model(lowerCamelCase       ).last_hidden_state

   set_seed(42       )  # Fixed seed at init time so the two models get the same random weights
   __A		:     Union[str, Any]												=		{"""type""": scaling_type, """factor""": 10.0}
   __A		:     int												=		OpenLlamaModel(lowerCamelCase       )
   scaled_model.to(lowerCamelCase       )
   scaled_model.eval()
   __A		:     Optional[Any]												=		scaled_model(lowerCamelCase       ).last_hidden_state
   __A		:     Optional[Any]												=		scaled_model(lowerCamelCase       ).last_hidden_state

   # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
   # maximum sequence length, so the outputs for the short input should match.
   if scaling_type == "dynamic":
     self.assertTrue(torch.allclose(lowerCamelCase				,   lowerCamelCase				,   atol=1e-5       )       )
   else:
     self.assertFalse(torch.allclose(lowerCamelCase				,   lowerCamelCase				,   atol=1e-5       )       )

   # The output should be different for long inputs
   self.assertFalse(torch.allclose(lowerCamelCase				,   lowerCamelCase				,   atol=1e-5       )       )





'''simple docstring'''
from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

A__   :		Any 		=logging.get_logger(__name__)







@add_end_docstrings(_SCREAMING_SNAKE_CASE			)
class  __A				( _SCREAMING_SNAKE_CASE			):
 def __init__(   self			:    Union[str, Any]				,   *lowerCamelCase			:    int				,   **lowerCamelCase			:    Optional[int]       ):



   """simple docstring"""





   super().__init__(*lowerCamelCase				,   **lowerCamelCase       )
   self.check_model_type(lowerCamelCase       )
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Dict=None				,   lowerCamelCase			:    Union[str, Any]=None				,   lowerCamelCase			:    int=None				,   **lowerCamelCase			:    int       ):



   """simple docstring"""





   __A							,					__A		:     Tuple												=		{}, {}
   if padding is not None:
     __A		:     Any												=		padding
   if truncation is not None:
     __A		:     Optional[Any]												=		truncation
   if top_k is not None:
     __A		:     List[str]												=		top_k
   return preprocess_params, {}, postprocess_params
 def __call__(   self			:    Any				,   lowerCamelCase			:    Union["Image.Image", str]				,   lowerCamelCase			:    str = None				,   **lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   if isinstance(lowerCamelCase				,   (Image.Image, str)       ) and isinstance(lowerCamelCase				,   lowerCamelCase       ):
     __A		:     Tuple												=		{"""image""": image, """question""": question}
   else:
     __A		:     List[Any]												=		image
   __A		:     Any												=		super().__call__(lowerCamelCase				,   **lowerCamelCase       )
   return results
 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Dict=False				,   lowerCamelCase			:    str=False       ):



   """simple docstring"""





   __A		:     List[str]												=		load_image(inputs["""image"""]       )
   __A		:     Optional[Any]												=		self.tokenizer(
       inputs["""question"""]				,   return_tensors=self.framework				,   padding=lowerCamelCase				,   truncation=lowerCamelCase       )
   __A		:     Union[str, Any]												=		self.image_processor(images=lowerCamelCase				,   return_tensors=self.framework       )
   model_inputs.update(lowerCamelCase       )
   return model_inputs
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   __A		:     List[str]												=		self.model(**lowerCamelCase       )
   return model_outputs






 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    List[Any]=5       ):



   """simple docstring"""





   if top_k > self.model.config.num_labels:
     __A		:     str												=		self.model.config.num_labels

   if self.framework == "pt":
     __A		:     Optional[Any]												=		model_outputs.logits.sigmoid()[0]
     __A							,					__A		:     int												=		probs.topk(lowerCamelCase       )
   else:
     raise ValueError(f"Unsupported framework: {self.framework}"       )

   __A		:     Optional[Any]												=		scores.tolist()
   __A		:     Dict												=		ids.tolist()
   return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase				,   lowerCamelCase       )]



