

import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
lowerCamelCase           =					{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}







@is_pipeline_test
class 						__magic_name__			(							unittest.TestCase							):

   '''simple docstring'''




   lowerCamelCase__ :	Optional[Any]    =   MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
   lowerCamelCase__ :	str    =   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

   if model_mapping is not None:
      lowerCamelCase__ :	int    =   {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
   if tf_model_mapping is not None:
      lowerCamelCase__ :	Tuple    =   {
          config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
      }



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_       )							->    List[Any]:






     """simple docstring"""






     a__  =ZeroShotClassificationPipeline(
         model=lowercase_,    tokenizer=lowercase_,    candidate_labels=['''polics''', '''health''']       )
     return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_       )							->    int:






     """simple docstring"""






     a__  =classifier('''Who are you voting for in 2020?''',    candidate_labels='''politics'''       )
     self.assertEqual(lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       )], '''scores''': [ANY(lowercase_       )]}       )

     # No kwarg
     a__  =classifier('''Who are you voting for in 2020?''',    ['''politics''']       )
     self.assertEqual(lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       )], '''scores''': [ANY(lowercase_       )]}       )

     a__  =classifier('''Who are you voting for in 2020?''',    candidate_labels=['''politics''']       )
     self.assertEqual(lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       )], '''scores''': [ANY(lowercase_       )]}       )

     a__  =classifier('''Who are you voting for in 2020?''',    candidate_labels='''politics, public health'''       )
     self.assertEqual(
         lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       ), ANY(lowercase_       )], '''scores''': [ANY(lowercase_       ), ANY(lowercase_       )]}       )
     self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']       )       ),    1.0       )

     a__  =classifier('''Who are you voting for in 2020?''',    candidate_labels=['''politics''', '''public health''']       )
     self.assertEqual(
         lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       ), ANY(lowercase_       )], '''scores''': [ANY(lowercase_       ), ANY(lowercase_       )]}       )
     self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']       )       ),    1.0       )

     a__  =classifier(
         '''Who are you voting for in 2020?''',    candidate_labels='''politics''',    hypothesis_template='''This text is about {}'''       )
     self.assertEqual(lowercase_,    {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       )], '''scores''': [ANY(lowercase_       )]}       )

     # https://github.com/huggingface/transformers/issues/13846
     a__  =classifier(['''I am happy'''],    ['''positive''', '''negative''']       )
     self.assertEqual(
         lowercase_,    [
             {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       ), ANY(lowercase_       )], '''scores''': [ANY(lowercase_       ), ANY(lowercase_       )]}
             for i in range(1       )
         ],    )
     a__  =classifier(['''I am happy''', '''I am sad'''],    ['''positive''', '''negative''']       )
     self.assertEqual(
         lowercase_,    [
             {'''sequence''': ANY(lowercase_       ), '''labels''': [ANY(lowercase_       ), ANY(lowercase_       )], '''scores''': [ANY(lowercase_       ), ANY(lowercase_       )]}
             for i in range(2       )
         ],    )

     with self.assertRaises(lowercase_       ):
       classifier('''''',    candidate_labels='''politics'''       )

     with self.assertRaises(lowercase_       ):
       classifier(lowercase_,    candidate_labels='''politics'''       )

     with self.assertRaises(lowercase_       ):
       classifier('''Who are you voting for in 2020?''',    candidate_labels=''''''       )

     with self.assertRaises(lowercase_       ):
       classifier('''Who are you voting for in 2020?''',    candidate_labels=lowercase_       )

     with self.assertRaises(lowercase_       ):
       classifier(
           '''Who are you voting for in 2020?''',    candidate_labels='''politics''',    hypothesis_template='''Not formatting template''',    )

     with self.assertRaises(lowercase_       ):
       classifier(
           '''Who are you voting for in 2020?''',    candidate_labels='''politics''',    hypothesis_template=lowercase_,    )

     self.run_entailment_id(lowercase_       )



   def  _UpperCAmelCase (				self,    lowercase_       )							->    int:






     """simple docstring"""






     a__  =zero_shot_classifier.model.config
     a__  =config.labelaid
     a__  =zero_shot_classifier.entailment_id

     a__  ={'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
     self.assertEqual(zero_shot_classifier.entailment_id,    -1       )

     a__  ={'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2}
     self.assertEqual(zero_shot_classifier.entailment_id,    0       )

     a__  ={'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
     self.assertEqual(zero_shot_classifier.entailment_id,    0       )

     a__  ={'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
     self.assertEqual(zero_shot_classifier.entailment_id,    2       )

     a__  =original_labelaid
     self.assertEqual(lowercase_,    zero_shot_classifier.entailment_id       )



   @require_torch
   def  _UpperCAmelCase (				self       )							->    List[Any]:






     """simple docstring"""






     a__  =pipeline(
         '''zero-shot-classification''',    model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''',    framework='''pt''',    )
     # There was a regression in 4.10 for this
     # Adding a test so we don't make the mistake again.
     # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
     zero_shot_classifier(
         '''Who are you voting for in 2020?''' * 100,    candidate_labels=['''politics''', '''public health''', '''science''']       )



   @require_torch
   def  _UpperCAmelCase (				self       )							->    Union[str, Any]:






     """simple docstring"""






     a__  =pipeline(
         '''zero-shot-classification''',    model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''',    framework='''pt''',    )
     a__  =zero_shot_classifier(
         '''Who are you voting for in 2020?''',    candidate_labels=['''politics''', '''public health''', '''science''']       )

     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': '''Who are you voting for in 2020?''',
             '''labels''': ['''science''', '''public health''', '''politics'''],
             '''scores''': [0.333, 0.333, 0.333],
         },    )



   @require_tf
   def  _UpperCAmelCase (				self       )							->    Dict:






     """simple docstring"""






     a__  =pipeline(
         '''zero-shot-classification''',    model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''',    framework='''tf''',    )
     a__  =zero_shot_classifier(
         '''Who are you voting for in 2020?''',    candidate_labels=['''politics''', '''public health''', '''science''']       )

     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': '''Who are you voting for in 2020?''',
             '''labels''': ['''science''', '''public health''', '''politics'''],
             '''scores''': [0.333, 0.333, 0.333],
         },    )



   @slow
   @require_torch
   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__  =pipeline('''zero-shot-classification''',    model='''roberta-large-mnli''',    framework='''pt'''       )
     a__  =zero_shot_classifier(
         '''Who are you voting for in 2020?''',    candidate_labels=['''politics''', '''public health''', '''science''']       )

     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': '''Who are you voting for in 2020?''',
             '''labels''': ['''politics''', '''public health''', '''science'''],
             '''scores''': [0.976, 0.015, 0.009],
         },    )
     a__  =zero_shot_classifier(
         '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
         ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
         ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
         ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
         ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
         ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
         ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
         ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
         ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
         ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
         ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
         ''' large and limited training data.''',    candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''],    multi_label=lowercase_,    )
     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': (
                 '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                 ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                 ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                 ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                 ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                 ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                 ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                 ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                 ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                 ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                 ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                 ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                 ''' limited training data.'''
             ),
             '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
             '''scores''': [0.817, 0.713, 0.018, 0.018],
         },    )



   @slow
   @require_tf
   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     a__  =pipeline('''zero-shot-classification''',    model='''roberta-large-mnli''',    framework='''tf'''       )
     a__  =zero_shot_classifier(
         '''Who are you voting for in 2020?''',    candidate_labels=['''politics''', '''public health''', '''science''']       )

     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': '''Who are you voting for in 2020?''',
             '''labels''': ['''politics''', '''public health''', '''science'''],
             '''scores''': [0.976, 0.015, 0.009],
         },    )
     a__  =zero_shot_classifier(
         '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
         ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
         ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
         ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
         ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
         ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
         ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
         ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
         ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
         ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
         ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
         ''' large and limited training data.''',    candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''],    multi_label=lowercase_,    )
     self.assertEqual(
         nested_simplify(lowercase_       ),    {
             '''sequence''': (
                 '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                 ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                 ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                 ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                 ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                 ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                 ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                 ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                 ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                 ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                 ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                 ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                 ''' limited training data.'''
             ),
             '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
             '''scores''': [0.817, 0.713, 0.018, 0.018],
         },    )



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

   '''simple docstring'''




   def __init__(				self,    lowercase_,    lowercase_=13,    lowercase_=7,    lowercase_=True,    lowercase_=True,    lowercase_=False,    lowercase_=True,    lowercase_=99,    lowercase_=32,    lowercase_=5,    lowercase_=4,    lowercase_=37,    lowercase_="gelu",    lowercase_=0.1,    lowercase_=0.1,    lowercase_=512,    lowercase_=16,    lowercase_=2,    lowercase_=0.02,    lowercase_=3,    lowercase_=4,    lowercase_=None,    )							->    List[Any]:






     """simple docstring"""






     a__  =parent
     a__  =batch_size
     a__  =seq_length
     a__  =is_training
     a__  =use_input_mask
     a__  =use_token_type_ids
     a__  =use_labels
     a__  =vocab_size
     a__  =hidden_size
     a__  =num_hidden_layers
     a__  =num_attention_heads
     a__  =intermediate_size
     a__  =hidden_act
     a__  =hidden_dropout_prob
     a__  =attention_probs_dropout_prob
     a__  =max_position_embeddings
     a__  =type_vocab_size
     a__  =type_sequence_label_size
     a__  =initializer_range
     a__  =num_labels
     a__  =num_choices
     a__  =scope



   def  _UpperCAmelCase (				self       )							->    Optional[Any]:






     """simple docstring"""






     a__  =ids_tensor([self.batch_size, self.seq_length],    self.vocab_size       )

     a__  =None
     if self.use_input_mask:
       a__  =random_attention_mask([self.batch_size, self.seq_length]       )

     a__  =None
     if self.use_token_type_ids:
       a__  =ids_tensor([self.batch_size, self.seq_length],    self.type_vocab_size       )

     a__  =None
     a__  =None
     a__  =None
     if self.use_labels:
       a__  =ids_tensor([self.batch_size],    self.type_sequence_label_size       )
       a__  =ids_tensor([self.batch_size, self.seq_length],    self.num_labels       )
       a__  =ids_tensor([self.batch_size],    self.num_choices       )

     a__  =self.get_config()

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



   def  _UpperCAmelCase (				self       )							->    List[str]:






     """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=lowercase_,    initializer_range=self.initializer_range,    use_stable_embedding=lowercase_,    )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    List[str]:






     """simple docstring"""






     a__  =OpenLlamaModel(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_       )
     a__  =model(lowercase_       )
     self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    Any:






     """simple docstring"""






     a__  =True
     a__  =OpenLlamaModel(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    )
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    )
     a__  =model(lowercase_,    attention_mask=lowercase_       )
     self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[str]:






     """simple docstring"""






     a__  =OpenLlamaForCausalLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.seq_length, self.vocab_size)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[Any]:






     """simple docstring"""






     a__  =True
     a__  =True
     a__  =OpenLlamaForCausalLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()

     # first forward pass
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    use_cache=lowercase_,    )
     a__  =outputs.past_key_values

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

     # append to next input_ids and
     a__  =torch.cat([input_ids, next_tokens],    dim=-1       )
     a__  =torch.cat([input_mask, next_mask],    dim=-1       )

     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    output_hidden_states=lowercase_,    )['''hidden_states'''][0]
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    past_key_values=lowercase_,    output_hidden_states=lowercase_,    )['''hidden_states'''][0]

     # select random slice
     a__  =ids_tensor((1,),    output_from_past.shape[-1]       ).item()
     a__  =output_from_no_past[:, -3:, random_slice_idx].detach()
     a__  =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(lowercase_,    lowercase_,    atol=1E-3       )       )



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     a__  =self.prepare_config_and_inputs()
     (
         (
         a__
     ),      (
         a__
     ),      (
         a__
     ),      (
         a__
     ),      (
         a__
     ),      (
         a__
     ),      (
         a__
     ),      
     )  =config_and_inputs
     a__  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}
     return config, inputs_dict







@require_torch
class 						__magic_name__			(							lowerCamelCase__		,       lowerCamelCase__		,       lowerCamelCase__		,       unittest.TestCase							):

   '''simple docstring'''




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



   def  _UpperCAmelCase (				self       )							->    List[str]:






     """simple docstring"""






     a__  =OpenLlamaModelTester(self       )
     a__  =ConfigTester(self,    config_class=lowercase_,    hidden_size=37       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     self.config_tester.run_common_tests()



   def  _UpperCAmelCase (				self       )							->    Optional[Any]:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
       a__  =type
       self.model_tester.create_and_check_model(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =3
     a__  =input_dict['''input_ids''']
     a__  =input_ids.ne(1       ).to(lowercase_       )
     a__  =ids_tensor([self.model_tester.batch_size],    self.model_tester.type_sequence_label_size       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.assertEqual(result.logits.shape,    (self.model_tester.batch_size, self.model_tester.num_labels)       )



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =3
     a__  ='''single_label_classification'''
     a__  =input_dict['''input_ids''']
     a__  =input_ids.ne(1       ).to(lowercase_       )
     a__  =ids_tensor([self.model_tester.batch_size],    self.model_tester.type_sequence_label_size       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.assertEqual(result.logits.shape,    (self.model_tester.batch_size, self.model_tester.num_labels)       )



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =3
     a__  ='''multi_label_classification'''
     a__  =input_dict['''input_ids''']
     a__  =input_ids.ne(1       ).to(lowercase_       )
     a__  =ids_tensor(
         [self.model_tester.batch_size, config.num_labels],    self.model_tester.type_sequence_label_size       ).to(torch.float       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     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  _UpperCAmelCase (				self       )							->    List[str]:






     """simple docstring"""






     pass



   @parameterized.expand([('''linear''',), ('''dynamic''',)]       )
   def  _UpperCAmelCase (				self,    lowercase_       )							->    Optional[Any]:






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =ids_tensor([1, 10],    config.vocab_size       )
     a__  =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__  =OpenLlamaModel(lowercase_       )
     original_model.to(lowercase_       )
     original_model.eval()
     a__  =original_model(lowercase_       ).last_hidden_state
     a__  =original_model(lowercase_       ).last_hidden_state

     set_seed(42       )  # Fixed seed at init time so the two models get the same random weights
     a__  ={'''type''': scaling_type, '''factor''': 10.0}
     a__  =OpenLlamaModel(lowercase_       )
     scaled_model.to(lowercase_       )
     scaled_model.eval()
     a__  =scaled_model(lowercase_       ).last_hidden_state
     a__  =scaled_model(lowercase_       ).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(lowercase_,    lowercase_,    atol=1E-5       )       )
     else:
       self.assertFalse(torch.allclose(lowercase_,    lowercase_,    atol=1E-5       )       )

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