
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     _UpperCamelCase			:






			'''simple docstring'''

			def __init__(    self	:    Tuple  ,		snake_case_	:    Tuple  ,		snake_case_	:    List[Any]=13  ,		snake_case_	:    List[str]=7  ,		snake_case_	:    List[Any]=True  ,		snake_case_	:    Union[str, Any]=True  ,		snake_case_	:    Union[str, Any]=False  ,		snake_case_	:    List[Any]=True  ,		snake_case_	:    Optional[int]=99  ,		snake_case_	:    Dict=32  ,		snake_case_	:    Union[str, Any]=5  ,		snake_case_	:    Any=4  ,		snake_case_	:    Optional[Any]=37  ,		snake_case_	:    Optional[Any]="gelu"  ,		snake_case_	:    str=0.1  ,		snake_case_	:    Any=0.1  ,		snake_case_	:    List[Any]=512  ,		snake_case_	:    Tuple=16  ,		snake_case_	:    Union[str, Any]=2  ,		snake_case_	:    Union[str, Any]=0.02  ,		snake_case_	:    Optional[Any]=3  ,		snake_case_	:    int=4  ,		snake_case_	:    List[Any]=None  ,		):
							UpperCamelCase_:							str		=  parent
							UpperCamelCase_:							List[Any]		=  batch_size
							UpperCamelCase_:							Optional[Any]		=  seq_length
							UpperCamelCase_:							Any		=  is_training
							UpperCamelCase_:							List[str]		=  use_input_mask
							UpperCamelCase_:							List[Any]		=  use_token_type_ids
							UpperCamelCase_:							Dict		=  use_labels
							UpperCamelCase_:							Union[str, Any]		=  vocab_size
							UpperCamelCase_:							Tuple		=  hidden_size
							UpperCamelCase_:							str		=  num_hidden_layers
							UpperCamelCase_:							Optional[int]		=  num_attention_heads
							UpperCamelCase_:							List[str]		=  intermediate_size
							UpperCamelCase_:							Optional[Any]		=  hidden_act
							UpperCamelCase_:							Optional[Any]		=  hidden_dropout_prob
							UpperCamelCase_:							str		=  attention_probs_dropout_prob
							UpperCamelCase_:							int		=  max_position_embeddings
							UpperCamelCase_:							Tuple		=  type_vocab_size
							UpperCamelCase_:							str		=  type_sequence_label_size
							UpperCamelCase_:							str		=  initializer_range
							UpperCamelCase_:							Dict		=  num_labels
							UpperCamelCase_:							Union[str, Any]		=  num_choices
							UpperCamelCase_:							Optional[int]		=  scope

			def  lowerCAmelCase__		(    self	:    str ):
							UpperCamelCase_:							Optional[Any]		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size )

							UpperCamelCase_:							Dict		=  None
							if self.use_input_mask:
											UpperCamelCase_:							List[Any]		=  random_attention_mask([self.batch_size, self.seq_length] )

							UpperCamelCase_:							Tuple		=  None
							if self.use_token_type_ids:
											UpperCamelCase_:							List[Any]		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.type_vocab_size )

							UpperCamelCase_:							str		=  None
							UpperCamelCase_:							int		=  None
							UpperCamelCase_:							Tuple		=  None
							if self.use_labels:
											UpperCamelCase_:							int		=  ids_tensor([self.batch_size]  ,		self.type_sequence_label_size )
											UpperCamelCase_:							int		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.num_labels )
											UpperCamelCase_:							Dict		=  ids_tensor([self.batch_size]  ,		self.num_choices )

							UpperCamelCase_:							Optional[int]		=  self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

			def  lowerCAmelCase__		(    self	:    Tuple ):
							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=snake_case_  ,		initializer_range=self.initializer_range  ,		use_stable_embedding=snake_case_  ,		)

			def  lowerCAmelCase__		(    self	:    Union[str, Any]  ,		snake_case_	:    Any  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Optional[Any] ):
							UpperCamelCase_:							List[str]		=  OpenLlamaModel(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Optional[Any]		=  model(snake_case_  ,		attention_mask=snake_case_ )
							UpperCamelCase_:							Dict		=  model(snake_case_ )
							self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size) )

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

			def  lowerCAmelCase__		(    self	:    List[str]  ,		snake_case_	:    Tuple  ,		snake_case_	:    Dict  ,		snake_case_	:    int  ,		snake_case_	:    List[str]  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Any  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Dict  ,		snake_case_	:    List[str]  ,		):
							UpperCamelCase_:							int		=  OpenLlamaForCausalLM(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							int		=  model(snake_case_  ,		attention_mask=snake_case_  ,		labels=snake_case_ )
							self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.vocab_size) )

			def  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    str  ,		snake_case_	:    List[str]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Tuple  ,		snake_case_	:    List[Any]  ,		snake_case_	:    List[str]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    List[str]  ,		):
							UpperCamelCase_:							List[Any]		=  True
							UpperCamelCase_:							Optional[Any]		=  True
							UpperCamelCase_:							List[Any]		=  OpenLlamaForCausalLM(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()

							# first forward pass
							UpperCamelCase_:							int		=  model(
							    snake_case_  ,		attention_mask=snake_case_  ,		encoder_hidden_states=snake_case_  ,		encoder_attention_mask=snake_case_  ,		use_cache=snake_case_  ,		)
							UpperCamelCase_:							Tuple		=  outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							UpperCamelCase_:							Tuple		=  ids_tensor((self.batch_size, 3)  ,		config.vocab_size )
							UpperCamelCase_:							Any		=  ids_tensor((self.batch_size, 3)  ,		vocab_size=2 )

							# append to next input_ids and
							UpperCamelCase_:							int		=  torch.cat([input_ids, next_tokens]  ,		dim=-1 )
							UpperCamelCase_:							List[str]		=  torch.cat([input_mask, next_mask]  ,		dim=-1 )

							UpperCamelCase_:							int		=  model(
							    snake_case_  ,		attention_mask=snake_case_  ,		encoder_hidden_states=snake_case_  ,		encoder_attention_mask=snake_case_  ,		output_hidden_states=snake_case_  ,		)["""hidden_states"""][0]
							UpperCamelCase_:							List[Any]		=  model(
							    snake_case_  ,		attention_mask=snake_case_  ,		encoder_hidden_states=snake_case_  ,		encoder_attention_mask=snake_case_  ,		past_key_values=snake_case_  ,		output_hidden_states=snake_case_  ,		)["""hidden_states"""][0]

							# select random slice
							UpperCamelCase_:							Union[str, Any]		=  ids_tensor((1,)  ,		output_from_past.shape[-1] ).item()
							UpperCamelCase_:							Tuple		=  output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCamelCase_:							Dict		=  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(snake_case_  ,		snake_case_  ,		atol=1e-3 ) )




			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							Dict		=  self.prepare_config_and_inputs()
							(
							    (
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				
							):							Optional[int]		=  config_and_inputs
							UpperCamelCase_:							List[Any]		=  {"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict




@require_torch
class     _UpperCamelCase			(   _A       ,    _A       ,    _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Dict    =	(
			    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
			)
			__UpperCamelCase :  Dict    =	(OpenLlamaForCausalLM,) if is_torch_available() else ()
			__UpperCamelCase :  List[str]    =	(
			    {
			        """feature-extraction""": OpenLlamaModel,
			        """text-classification""": OpenLlamaForSequenceClassification,
			        """text-generation""": OpenLlamaForCausalLM,
			        """zero-shot""": OpenLlamaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__UpperCamelCase :  Any    =	False
			__UpperCamelCase :  Optional[Any]    =	False

			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							Optional[Any]		=  OpenLlamaModelTester(self )
							UpperCamelCase_:							Optional[int]		=  ConfigTester(self  ,		config_class=snake_case_  ,		hidden_size=37 )

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							self.config_tester.run_common_tests()

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Optional[int]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Tuple ):
							UpperCamelCase_:							int		=  self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											UpperCamelCase_:							Optional[int]		=  type
											self.model_tester.create_and_check_model(*snake_case_ )

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

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

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_,				UpperCamelCase_:							Dict		=  self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase_:							Optional[Any]		=  3
							UpperCamelCase_:							List[Any]		=  """multi_label_classification"""
							UpperCamelCase_:							str		=  input_dict["""input_ids"""]
							UpperCamelCase_:							int		=  input_ids.ne(1 ).to(snake_case_ )
							UpperCamelCase_:							str		=  ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]  ,		self.model_tester.type_sequence_label_size ).to(torch.float )
							UpperCamelCase_:							Optional[Any]		=  OpenLlamaForSequenceClassification(snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Optional[Any]		=  model(snake_case_  ,		attention_mask=snake_case_  ,		labels=snake_case_ )
							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  lowerCAmelCase__		(    self	:    str ):
							pass




			@parameterized.expand([("""linear""",), ("""dynamic""",)] )
			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    int ):
							UpperCamelCase_,				UpperCamelCase_:							Optional[int]		=  self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase_:							Dict		=  ids_tensor([1, 10]  ,		config.vocab_size )
							UpperCamelCase_:							Optional[int]		=  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
							UpperCamelCase_:							Tuple		=  OpenLlamaModel(snake_case_ )
							original_model.to(snake_case_ )
							original_model.eval()
							UpperCamelCase_:							Tuple		=  original_model(snake_case_ ).last_hidden_state
							UpperCamelCase_:							List[Any]		=  original_model(snake_case_ ).last_hidden_state

							set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
							UpperCamelCase_:							List[str]		=  {"""type""": scaling_type, """factor""": 10.0}
							UpperCamelCase_:							str		=  OpenLlamaModel(snake_case_ )
							scaled_model.to(snake_case_ )
							scaled_model.eval()
							UpperCamelCase_:							str		=  scaled_model(snake_case_ ).last_hidden_state
							UpperCamelCase_:							Union[str, Any]		=  scaled_model(snake_case_ ).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(snake_case_  ,		snake_case_  ,		atol=1e-5 ) )
							else:
											self.assertFalse(torch.allclose(snake_case_  ,		snake_case_  ,		atol=1e-5 ) )

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








import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow

@is_pipeline_test
@require_torch
class     _UpperCamelCase			(   unittest.TestCase					):






			'''simple docstring'''

			@require_torch
			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							Any		=  pipeline(
							    task="""zero-shot-audio-classification"""  ,		model="""hf-internal-testing/tiny-clap-htsat-unfused""" )
							UpperCamelCase_:							str		=  load_dataset("""ashraq/esc50""" )
							UpperCamelCase_:							Optional[int]		=  dataset["""train"""]["""audio"""][-1]["""array"""]
							UpperCamelCase_:							Optional[int]		=  audio_classifier(snake_case_  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[{"""score""": 0.501, """label""": """Sound of a dog"""}, {"""score""": 0.499, """label""": """Sound of vaccum cleaner"""}]  ,		)

			@unittest.skip("""No models are available in TF""" )
			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							pass

			@slow
			@require_torch
			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_:							List[str]		=  pipeline(
							    task="""zero-shot-audio-classification"""  ,		model="""laion/clap-htsat-unfused"""  ,		)
							# This is an audio of a dog
							UpperCamelCase_:							Tuple		=  load_dataset("""ashraq/esc50""" )
							UpperCamelCase_:							Optional[int]		=  dataset["""train"""]["""audio"""][-1]["""array"""]
							UpperCamelCase_:							int		=  audio_classifier(snake_case_  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )

							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        {"""score""": 0.999, """label""": """Sound of a dog"""},
							        {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							    ]  ,		)

							UpperCamelCase_:							Any		=  audio_classifier([audio] * 5  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        [
							            {"""score""": 0.999, """label""": """Sound of a dog"""},
							            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							        ],
							    ]
							    * 5  ,		)
							UpperCamelCase_:							Any		=  audio_classifier(
							    [audio] * 5  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""]  ,		batch_size=5 )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        [
							            {"""score""": 0.999, """label""": """Sound of a dog"""},
							            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							        ],
							    ]
							    * 5  ,		)




			@unittest.skip("""No models are available in TF""" )
			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							pass






